What changes when you swap sort() for toSorted(), with a real 1-million-item Node.js benchmark and the support table.
3 min read
toSorted vs sort JavaScript in one line: sort() reorders the array you call it on and returns that same array, while toSorted() leaves the original alone and returns a sorted copy. Both follow the same compare function rules. toSorted() arrived with ECMAScript 2023 and works in Chrome 110+, Firefox 115+, Safari 16+ and Node.js 20+.
MDN describes toSorted() as the copying version of sort():
toSorted() came from the TC39 Change Array by Copy proposal, which also added toReversed(), toSpliced() and with(). TC39's finished-proposals list shows it shipped in 2023.
Not in our test. We sorted 1,000,000 random numbers with (a, b) => a - b in Node.js v20.19.2, best of 7 runs:
All four are within about 4 percent of each other, which is run-to-run noise on our machine. If you were already copying before sorting, toSorted() does the same job in one call.
Only if you give it a compare function, exactly like sort(). Without one, both convert items to strings and compare them in UTF-16 order. In our run, [1, 10, 21, 2].toSorted() returned [1, 10, 2, 21]. Use toSorted((a, b) => a - b) for numbers.
They differ. MDN says sort() keeps empty slots and moves them to the end, while toSorted() treats empty slots as undefined. We checked in Node: sorting [3, , 1] with sort() left a real hole at the end, but toSorted() returned [1, 3, undefined] with no hole.
React's docs say to treat arrays in state as read-only and list sort() among the methods to avoid, because it mutates the array; their example copies first with [...list]. toSorted() gives you the copy and the sort in one step. It also works on frozen arrays: in our check, toSorted() on Object.freeze([3, 1, 2]) returned [1, 2, 3], while sort() on the same array threw a TypeError.
MDN's compatibility data lists Chrome and Edge 110, Firefox 115, Safari 16, Node.js 20.0.0, Deno 1.31 and Bun 1.0. MDN marks it Baseline widely available since July 2023. Internet Explorer has no support.
Is toSorted() stable? MDN notes that sort() has been required to be stable since ECMAScript 2019, and toSorted() is its copying version, following the same sorting rules.
Is toSorted() a deep copy? No. It returns a new array, but objects inside it are the same objects as in the original.
Do typed arrays have toSorted()? Yes. The proposal also added toSorted() to typed arrays, and MDN documents TypedArray.prototype.toSorted().
Lab evidence
Checked on 18 Oct 2026 against MDN's toSorted() and sort() reference pages, MDN browser-compat data, the TC39 Change Array by Copy proposal and finished-proposals list, and React's Updating Arrays in State guide. We also ran a real benchmark and behaviour checks on 18 Oct 2026 in Node.js v20.19.2 on an 8-vCPU x86_64 Linux box (1,000,000 seeded random numbers, best of 7 runs). Timings are from that one machine and will differ on yours.
Explore the product
Freelancers and devs who want to turn coding skills into paid site builds
We ran three typescript-eslint presets on a real open-source TypeScript library and counted every finding and second. The jump to strict was smaller than the jump to type-aware linting, and the findings themselves tell you whether your team is ready.
Summary4 Nov 20267 min read
Both cut a string at a separator, but they fail in different ways. We ran them on Python 3.13.5 to show where partition() is safer and where split() is the right tool.
Summary2 Nov 20263 min read
Dragging videos is the official way to change a playlist's order. Here are the steps from YouTube Help, where to find them in YouTube Studio, and the cases where reordering will not work.
Summary31 Oct 20263 min read