Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.sort vs array.toSorted (2)
(version: 1)
Comparing performance of:
array.sort vs array.toSorted
Created:
11 months 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.sort
const result = array.sort(compareFn)
array.toSorted
const result = array.toSorted(compareFn)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.sort
array.toSorted
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.sort
12002.2 Ops/sec
array.toSorted
14058.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 11 months ago):
The benchmark you have provided compares two methods for sorting an array in JavaScript: `Array.prototype.sort()` and `Array.prototype.toSorted()`. Here's a breakdown of the test and some considerations regarding each approach: ### Benchmark Overview - **Name**: `array.sort vs array.toSorted` - **Description**: Not provided, but the focus is on comparing the performance of sorting methods. - **Preparation Script**: - An array is created with 10,000 random numbers using `Array.from()` and `Math.random()`. - A comparison function `compareFn` is defined to sort the numbers in ascending order. ### Test Cases 1. **Test Name**: `array.sort` - **Definition**: `const result = array.sort(compareFn)` - This method sorts the array **in place**, meaning it modifies the original array and also returns a reference to the same array. 2. **Test Name**: `array.toSorted` - **Definition**: `const result = array.toSorted(compareFn)` - Introduced in ECMAScript 2022, this method sorts the array **without modifying the original array**. Instead, it returns a new sorted array. ### Pros and Cons #### 1. `array.sort()` - **Pros**: - More widely supported across browsers and environments. - Performs well for smaller arrays due to direct in-place modification. - **Cons**: - Modifies the original array, which can lead to unintended side effects if the array is used elsewhere in the code after sorting. - Potentially less readable in some contexts, where immutability is preferred. #### 2. `array.toSorted()` - **Pros**: - Does not alter the original array, which adheres to functional programming practices where immutability can lead to safer and clearer code. - More intuitive in scenarios where you need a non-destructive method for sorting. - **Cons**: - Slightly less performant for large arrays since it creates a new array (more memory usage). - Newly added in ECMAScript 2022, which may lead to lower compatibility with older environments or legacy code. ### Performance Results Based on the latest benchmark results: - **`array.toSorted` Executions per Second**: 14,057.99 - **`array.sort` Executions per Second**: 12,002.23 This indicates that in the tested environment (Chrome 137 on macOS), `array.toSorted` outperforms `array.sort`. However, actual performance may vary across different browsers and implementations. ### Other Considerations - Depending on the type and size of the dataset, one method may be preferable over the other. For example, if you are frequently sorting without needing to keep the original array intact, `toSorted` may be a better choice. - The choice of method may also be influenced by the application's architecture (e.g., if functional programming patterns are favored). ### Alternatives Other alternatives for sorting in JavaScript could include: - Implementing custom sorting algorithms (like quicksort or mergesort) if specific sorting logic is needed, particularly for specialized data types. - Utilizing libraries like lodash or Underscore.js that provide their own sort functions (`_.sortBy`), which often come with additional features or optimizations. In conclusion, the choice between `array.sort()` and `array.toSorted()` depends on the specific requirements of your project concerning performance, side effects, and maintainability. Each provides its unique advantages that cater to different programming paradigms and needs.
Related benchmarks:
Length Check Sort vs Empty Sort
array vs Float64Array sort
math.min/max vs sort
[...array].sort vs array.toSorted
sort vs array.toSorted
array.sort vs array.toSorted
array.toSorted vs array.sort
array.sort vs array.toSortedxx
[...array].sort vs array.toSorted short array
Comments
Confirm delete:
Do you really want to delete benchmark?