Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array shift/spread test
(version: 0)
Comparing performance of:
unsift array vs spread array
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
unsift array
let arr = Array.from(Array(10000).keys()) for(i=0; i<10000; i++){ arr.unshift(i); } console.log(arr);
spread array
let arr = Array.from(Array(10000).keys()) for(i=0; i<10000; i++){ arr = [i, ...arr]; } console.log(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unsift array
spread array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unsift array
69.6 Ops/sec
spread array
1.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in this JavaScript microbenchmark. **Benchmark Overview** The benchmark measures the performance difference between two approaches: using `unshift()` and spreading an array (`...arr`) when adding elements at the beginning of an array. **Options Compared** There are two options being compared: 1. **Using `unshift()`**: This approach uses the `unshift()` method to add a new element to the beginning of the array. 2. **Spreading an array (`...arr`)**: This approach spreads the existing array and then adds a new element at the beginning. **Pros and Cons** * Using `unshift()`: This approach is more efficient because it only requires a single operation (pushing the new element onto the stack) followed by a loop to shift all elements down. However, this method can lead to slower performance due to the overhead of the array's internal indexing. * Spreading an array (`...arr`): This approach involves creating a new array and then adding the existing elements at the beginning. While it requires more operations (creating a new array), it avoids the overhead of array indexing. **Library Used** None, as this is a built-in JavaScript feature. **Special JS Feature/Syntax** No special JS features or syntax are used in these benchmark cases. **Benchmark Preparation Code** The preparation code for each test case involves creating an initial array of 10,000 elements using `Array.from()` and the `keys()` method. This ensures that both tests have a consistent starting point. **Individual Test Cases** Each test case executes a loop that either adds elements to the beginning of the array using `unshift()` or spreads the existing array and then adds new elements at the beginning. **Benchmark Results** The benchmark results show the performance of each approach: * Using `unshift()`: The "unsift array" test shows an execution rate of approximately 69.6 executions per second. * Spreading an array (`...arr`): The "spread array" test shows a much slower execution rate of approximately 1.7 executions per second. **Other Alternatives** There are other approaches that could be used to add elements at the beginning of an array, such as: * Using `concat()` and creating a new array * Using a `for` loop with indexing to access individual elements * Using a library like Lodash's `chunk()` function However, these alternatives may not provide the same level of performance or simplicity as the two approaches being compared in this benchmark.
Related benchmarks:
Spread Operator: Array
Push vs LHS spread
Move item in first place spread vs unshift
Move item in first place unshift vs spread
Spread vs Slice operators in JS
Comments
Confirm delete:
Do you really want to delete benchmark?