Skip to content

vue-doctor/performance/js-tosorted-immutable ​

Use array.toSorted() instead of [...array].sort(): same cost, clearer intent. Requires an ES2023 build target

PropertyValue
CategoryPerformance
Default Severitywarning
Confidencemedium
FixableNo
Engineoxlint
Sincev1.0.0
Frameworksvue, nuxt
Test filesNot checked (which files)

Why it matters ​

Use array.toSorted() instead of [...array].sort(): same cost, clearer intent. Requires an ES2023 build target

How to fix ​

Review the finding and update the code according to best practices below.

Agent Guidance ​

This is a readability rewrite, not a speed-up: toSorted copies the array just like the spread does. Replace [...array].sort(compare) with array.toSorted(compare) only when the receiver is a real array (Sets, Maps and iterators have no toSorted) and the build target supports ES2023 (Chrome 110, Firefox 115, Safari 16); Vite does not polyfill it. Leave the spread when either is not the case.


Rule source: vue-doctor/performance/js-tosorted-immutable

Released under the MIT License.