Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.toSorted vs array.sort
(version: 1)
Comparing performance of:
array.toSorted vs array.sort
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({ length: 10_000 }).map((_) => Math.random()); var compareFn = (a, b) => a - b
Tests:
array.toSorted
const result = array.toSorted(compareFn)
array.sort
const result = array.sort(compareFn);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.toSorted
array.sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.toSorted
376.6 Ops/sec
array.sort
5324.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares two methods of sorting arrays in JavaScript: `array.sort()` and `array.toSorted()`. This test evaluates the performance difference between these two sorting methods in terms of how many times they can be executed per second. ### What’s Tested 1. **`array.sort(compareFn)`**: - This method sorts the elements of an array in place and returns the sorted array. It modifies the original array and accepts a comparison function that defines the sort order. 2. **`array.toSorted(compareFn)`**: - This is a newer method introduced in the ECMAScript 2022 specification. It sorts the array and returns a new sorted array, leaving the original array unchanged. Like `array.sort()`, it also accepts a comparison function for custom sort orders. ### Pros and Cons #### `array.sort()` - **Pros**: - Widely supported across all JavaScript environments, making it a safe choice for generic applications. - In-place sorting means it doesn’t require additional memory to hold a new sorted array. - **Cons**: - Mutates the original array, which can lead to unintended side effects if the original data is needed afterward. - May lead to issues in codebases where immutability is preferred or required. #### `array.toSorted()` - **Pros**: - Does not mutate the original array, allowing developers to maintain the integrity of the original data. - More expressive and clearer intention when the original array should be preserved. - **Cons**: - As a newer method, it may not be supported in older browsers or environments not updated to the latest ECMAScript specification. - It potentially requires more memory, as it creates a new sorted array instead of modifying the existing one. ### Performance Considerations In the benchmark results: - **`array.sort()` executed 4729.66 times per second**. - **`array.toSorted()` executed only 796.33 times per second**. This indicates that `array.sort()` performs significantly better in this particular benchmark, likely due to its in-place sorting mechanism compared to the memory overhead of creating a new sorted array with `array.toSorted()`. Depending on the context of usage, if performance is critical and the data does not need to be preserved, `array.sort()` may be favored. However, if immutability is required or preferred, `array.toSorted()` is the better choice despite the performance tradeoff. ### Alternatives Other alternatives for sorting arrays in JavaScript include: - **Using custom sorting algorithms**: Developers can implement their own sorting logic (like QuickSort or MergeSort) for specific data types or conditions. - **Utilizing other libraries**: Libraries like Lodash offer sorting functions that may add additional features or optimizations. - **Functional programming approaches**: If immutability is desired, JavaScript does support functional paradigms that can facilitate such operations without mutating the original data (though typically at the cost of performance). In summary, this benchmark provides a straightforward comparison between two critical sorting methods in JavaScript, illustrating usage scenarios, performance implications, and considerations regarding immutability.
Related benchmarks:
sort vs reduce
sort vs reduce
sort v reduce
array vs Float64Array (small) 2
array vs Float64Array sort
math.min/max vs sort
[...array].sort vs array.toSorted
sort vs array.toSorted
array.sort vs array.toSorted
Comments
Confirm delete:
Do you really want to delete benchmark?