Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
const array = Array.from(Array(10000).keys()) const sortedArray = [...array].sort()
(version: 0)
Comparing performance of:
Copy via spread vs Copy via slice
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
window.sourceArr = Array.from(Array(10000).keys()) for (const i of window.sourceArr) { window.sourceArr[i] = Math.random(); }
Tests:
Copy via spread
const sortedArray = [...window.sourceArr].sort()
Copy via slice
const sortedArray = window.sourceArr.slice().sort()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Copy via spread
Copy via slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Copy via spread
224.5 Ops/sec
Copy via slice
225.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided JSON represents two benchmark test cases that measure the performance of copying an array using different methods: `Array.prototype.sort()` and `slice()`. The benchmarks are designed to test the execution speed of these methods when used with a large array of 10,000 elements. **Options Compared** There are two main options being compared: 1. **Copy via spread**: This option uses the spread operator (`...`) to create a new array from an existing one, and then sorts it. 2. **Copy via slice**: This option uses the `slice()` method to create a shallow copy of the original array, and then sorts it. **Pros and Cons** * **Copy via spread**: + Pros: Efficient use of memory, as it creates a new array without modifying the original one. + Cons: Can be slower due to the creation of a new array, which can lead to increased memory allocation and garbage collection overhead. * **Copy via slice**: + Pros: Faster execution speed, as it only creates a shallow copy of the array, which is more efficient than creating a new array. + Cons: Modifies the original array, which might not be desirable in some cases. **Library** The benchmark uses Lodash.js, a popular JavaScript library that provides a comprehensive set of functional programming helpers. In this case, it's used to provide the `Array.prototype.sort()` method, which is being compared against the `slice()` method. **Special JS Feature or Syntax** There doesn't appear to be any special JavaScript feature or syntax being used in these benchmarks. The focus is solely on comparing the performance of two different array manipulation methods. **Other Alternatives** If you were to write your own benchmark, you could consider using other methods for copying arrays, such as: * Using `Array.prototype.slice()` with a custom callback function * Using `Array.prototype.map()` followed by `Array.prototype.concat()` * Using a library like Ramda or Immutable.js However, these alternatives might not be relevant to the specific use case of measuring array sorting performance. **Benchmark Preparation Code** The preparation code generates a large array of 10,000 random numbers and assigns it to the `window.sourceArr` variable. This is done using two lines of code: ```javascript window.sourceArr = Array.from(Array(10000).keys()) for (const i of window.sourceArr) { window.sourceArr[i] = Math.random(); } ``` This code creates a new array with 10,000 elements and then assigns each element a random value. The resulting array is used as the input for the benchmark. **Individual Test Cases** The two test cases are: 1. **Copy via spread**: This test case uses the spread operator (`...`) to create a new array from `window.sourceArr` and sorts it. 2. **Copy via slice**: This test case uses the `slice()` method to create a shallow copy of `window.sourceArr` and sorts it. These test cases are designed to measure the execution speed of each method, allowing users to compare their performance and choose the best approach for their specific use case.
Related benchmarks:
Lodash sort vs array.prototype.sort - 2
Sort lodash vs native
Uniq by sorting test
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?