ShopperCove
Menu
All writingBlogTopicsCategoriesAboutRSS
Blog
Categories
Observability & SRE62All categories
Topics
sre111localhost lab102performance70python58javascript47bundle size36benchmarking35node.js35web development21typescript19benchmark16content creation15All topics
About
Loading article…

ShopperCove

Hands-on notes on developer tools, written after the tool has actually been run.

All writingBlogCategoriesTopicsAboutPrivacyRSS

© 2026 ShopperCove

On this page

  1. What is the difference between toSorted() and sort()?
  2. Is toSorted() slower than sort()?
  3. Does toSorted() sort numbers correctly?
  4. How do the two handle sparse arrays?
  5. Why is toSorted() handy in React and with frozen arrays?
  6. Which browsers and Node versions support toSorted()?
  7. FAQ
  8. Related
  9. Sources
  1. Blog

toSorted vs sort JavaScript: Which One Copies Your Array?

What changes when you swap sort() for toSorted(), with a real 1-million-item Node.js benchmark and the support table.

1 November 2026·3 min read

Summary

Disclosure

This post contains affiliate links. If you buy through them, I may earn a commission at no extra cost to you. I only make firsthand claims about products I have actually used.

On this page
  1. What is the difference between toSorted() and sort()?
  2. Is toSorted() slower than sort()?
  3. Does toSorted() sort numbers correctly?
  4. How do the two handle sparse arrays?
  5. Why is toSorted() handy in React and with frozen arrays?
  6. Which browsers and Node versions support toSorted()?
  7. FAQ
  8. Related
  9. Sources

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+.

What is the difference between toSorted() and sort()?

MDN describes toSorted() as the copying version of sort():

  • sort() sorts in place and returns a reference to the original array, now sorted. No copy is made.
  • toSorted() returns a new array with the elements sorted and does not touch the original.
const a = [3, 1, 2];
const b = a.toSorted(); // b is [1, 2, 3], a is still [3, 1, 2]
a.sort();               // a is now [1, 2, 3]

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.

Is toSorted() slower than sort()?

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:

  • [...arr].sort(): 425.1 ms
  • arr.slice().sort(): 407.6 ms
  • arr.toSorted(): 409.1 ms
  • sort() on a fresh copy, timing only the sort: 415.3 ms

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.

Does toSorted() sort numbers correctly?

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.

How do the two handle sparse arrays?

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.

Why is toSorted() handy in React and with frozen arrays?

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.

Which browsers and Node versions support toSorted()?

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.

FAQ

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().

Related

  • klona vs structuredClone
  • Immer vs Mutative
  • es-toolkit vs Lodash

Sources

  • 01developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted
  • 02developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
  • 03github.com/tc39/proposal-change-array-by-copy
  • 04github.com/tc39/proposals/blob/main/finished-proposals.md
  • 05react.dev/learn/updating-arrays-in-state

Lab evidence

What I found running this

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

Put your JavaScript skills into paid client sites

Browse Ready Client Sites

Notes when a lab post goes up

Occasional email for new hands-on reviews. No sequence and no sponsors.

Explore the product

Freelancers and devs who want to turn coding skills into paid site builds

Put your JavaScript skills into paid client sites

Browse Ready Client Sites

Keep reading

  • Is typescript-eslint Strict Worth 4x Errors?

    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

  • Python partition vs split: Which One Should You Use?

    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

  • How to Reorder a YouTube Playlist, and Why Won't It Drag?

    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