Sub topic 3
Chaining copying methods into pipelines
This chapter assumes you already know toSorted, toSpliced, and with. It also assumes you understand how shallow copies share object references.
Chaining copying methods
Each copying method returns a new array, so you can call the next method directly on the result. The original array is never touched, at any step.
const scores = [40, 100, 1, 5];
const result = scores
.toSorted((a, b) => a - b) // [1, 5, 40, 100]
.toSpliced(0, 1) // [5, 40, 100]
.with(0, 99); // [99, 40, 100]
console.log(result); // [99, 40, 100]
console.log(scores); // [40, 100, 1, 5]Each step allocates an intermediate array. For small UI lists the cost is negligible. In hot loops over large arrays, a single manual copy may be cheaper.
Updating immutable state in React and Redux
React and Redux detect changes by comparing references. If you mutate an array in place, the reference stays the same and the update can be missed. The with method gives you a new array. Object spread gives you a new element.
const todos = [
{ id: 1, text: "Write", done: false },
{ id: 2, text: "Review", done: false }
];
const next = todos.with(0, { ...todos[0], done: true });
console.log(next !== todos); // true
console.log(todos[0].done); // falseRuntime support and fallbacks
The copying methods are available in Node 20+ and in current browsers. For older runtimes, check whether the method exists and fall back to copy-then-mutate:
function sortedCopy(arr, compare) {
if (typeof Array.prototype.toSorted === "function") {
return arr.toSorted(compare);
}
return [...arr].sort(compare);
}
console.log(sortedCopy([3, 1, 2], (a, b) => a - b)); // [1, 2, 3]Edge cases
Sparse arrays
Holes are read as undefined, and the result is always a dense array. toSorted places undefined values at the end.
const sparse = [3, , 1];
console.log(sparse.toSorted()); // [1, 3, undefined]
console.log(1 in sparse.with(0, 9)); // trueArray-like receivers
The methods are generic. They work on any object that has a length property. They ignore Symbol.species and always return a plain Array.
const arrayLike = { length: 2, 0: "b", 1: "a" };
const out = Array.prototype.toSorted.call(arrayLike);
console.log(out, Array.isArray(out)); // ["a", "b"] trueTyped arrays
Typed arrays have toSorted, toReversed, and with, and they sort numerically by default. They have no toSpliced, because the length of a typed array is fixed.
const bytes = new Uint8Array([3, 1, 2]);
console.log(bytes.toSorted()); // Uint8Array [1, 2, 3]
console.log(typeof Uint8Array.prototype.toSpliced); // "undefined"Exercise
What does [5, , 2].with(1, 7) return, and what does [10, 9, 1].toSorted() return?
Show answer
The first returns [5, 7, 2], and the original array still has its hole. The second returns [1, 10, 9], because a plain Array sorts as strings when there is no comparator.
Exercise
Why can't you call toSpliced on a Float64Array?
Show answer
Typed arrays have a fixed length, and toSpliced can change the length. For that reason it is not defined on typed arrays.