Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs unshift
(version: 0)
Comparing performance of:
spread vs unshift
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(86400).fill('');
Tests:
spread
arr = ['', ...arr]
unshift
arr.unshift('')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
unshift
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
1760.0 Ops/sec
unshift
59899.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this specific benchmark. **Benchmark Overview** The benchmark is designed to compare two approaches: using the spread operator (`...`) to add elements to an array, and using the `unshift()` method to add elements to the beginning of an array. The benchmark aims to determine which approach is faster and more efficient. **Options Compared** There are only two options being compared: 1. **Spread Operator (`...`)**: This operator creates a new array by spreading the existing array's elements. In this case, it adds 86400 empty strings to the end of the original array. 2. **Unshift() Method**: This method adds one or more elements to the beginning of an array. **Pros and Cons** * **Spread Operator (`...`)**: + Pros: Creates a new array, which can be beneficial for certain use cases (e.g., when working with large datasets). + Cons: Requires creating a new array, which can lead to increased memory usage. * **Unshift() Method**: + Pros: Modifies the original array in place, reducing memory allocation and deallocation overhead. + Cons: Can be slower than using a spread operator for large arrays. **Library and Syntax** There is no specific library used in this benchmark. However, JavaScript's built-in `Array` methods are being utilized. **Special JS Feature or Syntax (Not applicable)** No special features or syntax are being tested or utilized in this benchmark. **Alternative Approaches** Other approaches that could be used to compare the performance of adding elements to an array include: * Using the `concat()` method instead of the spread operator. * Using a loop to add elements to the array (e.g., `arr.push('')`). * Using a library like Lodash or Ramda, which provide additional utility functions for working with arrays. **Benchmark Preparation Code** The preparation code creates an empty array with 86400 elements and assigns it to the variable `arr`. This ensures that both tests are starting from the same initial state. **Individual Test Cases** Each test case has a unique benchmark definition and test name: * The first test case uses the spread operator (`arr = ['', ...arr]`) and is named "spread". * The second test case uses the `unshift()` method (`arr.unshift('')`) and is named "unshift". **Latest Benchmark Result** The latest results show that Chrome 107 on a Desktop device with Windows operating system has performed the following execution rates: * For the "unshift" test: 34928.99609375 executions per second. * For the "spread" test: 1396.9669189453125 executions per second. This suggests that, in this specific scenario, using `unshift()` is significantly faster than using the spread operator for adding elements to an array.
Related benchmarks:
parseInt vs Number addition
toFixed vs Math.round() - result as a number
parseFloat(toFixed) vs Math.round()
fill test
Array.from vs Spread using 10000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?