Sub topic 13
How holes in sparse arrays are handled
This chapter assumes you know how toSorted, toSpliced, and with work from the earlier chapters. You should also know that an array index can exist with the value undefined or be missing entirely.
What a hole is
A sparse array has one or more missing indices. Missing indices are called holes. A hole is different from a slot that holds undefined. The in operator tells the two apart:
const sparse = [3, , 1];
const dense = [3, undefined, 1];
console.log(sparse.length); // 3
console.log(1 in sparse); // false (index 1 is a hole)
console.log(1 in dense); // true (index 1 holds undefined)Older array methods treat holes inconsistently. forEach, map, and filter skip them. map keeps the hole in its result. sort moves holes to the very end and leaves them as holes.
The rule for copying methods
The ES2023 copying methods follow one rule. They read every index from 0 to length - 1, and a hole reads as undefined. They then write each value into a new array. The result is always dense, with no holes.
const sparse = [3, , 1];
console.log(sparse.toSorted()); // [1, 3, undefined]
console.log(sparse.with(0, 9)); // [9, undefined, 1]
console.log(sparse.toSpliced(0, 1)); // [undefined, 1]
console.log(sparse.toReversed()); // [1, undefined, 3]
console.log(1 in sparse.with(0, 9)); // true
console.log(sparse); // [3, <1 empty item>, 1] (unchanged)Comparing with the mutating methods
| Operation | Mutating version | Copying version |
|---|---|---|
| Sort `[3, , 1]` | `[1, 3, <empty>]`, hole kept at end | `[1, 3, undefined]` |
| Replace index 0 | `arr[0] = 9` keeps the hole | `with(0, 9)` fills it with `undefined` |
| Remove index 0 | `splice(0, 1)` keeps the hole | `toSpliced(0, 1)` gives `[undefined, 1]` |
You can check the sort row yourself:
const inPlace = [3, , 1];
inPlace.sort();
console.log(2 in inPlace); // false (still a hole)
const copy = [3, , 1].toSorted();
console.log(2 in copy); // true (now a real undefined)Undefined and the comparator
toSorted handles undefined the same way sort does. Every undefined value goes to the end of the result, and the comparator is never called with undefined. That is why a numeric comparator does not produce NaN comparisons here:
const scores = [40, , 5, undefined, 100];
const sorted = scores.toSorted((a, b) => {
console.log("compare", a, b); // never logs undefined
return a - b;
});
console.log(sorted); // [5, 40, 100, undefined, undefined]Why this matters
Sparse arrays are rare in application code, but they can come from new Array(n), from delete arr[i], or from assigning past the end of an array. Code that checks i in arr or uses Object.keys(arr) sees different keys after you switch from sort to toSorted. When you replace mutating calls with copying ones, check whether any code relied on holes staying holes.
Exercise
Given const a = new Array(3); a[1] = "x";, what do a.toSorted() and 0 in a.with(1, "y") return?
Show answer
a.toSorted() returns ["x", undefined, undefined]. The two holes are read as undefined and sorted to the end. 0 in a.with(1, "y") is true, because with returns a dense array where index 0 holds a real undefined.
Exercise
Why does [2, , 1].toSorted((a, b) => a - b) not produce NaN comparisons, even though the hole becomes undefined?
Show answer
The sort algorithm moves undefined values to the end before it calls the comparator. The comparator only ever receives 2 and 1, and the result is [1, 2, undefined].