Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Big Array prepend
(version: 0)
Comparing performance of:
Spread Operator vs Unshift vs Unshift without mutation vs Prepend function
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a; var prependArray = function(value, oldArray) { var i, len = oldArray.length + 1, newArray = new Array(len); newArray[0] = value; for (i = 0; i < len; ++i) { newArray.push(oldArray[i]); } return newArray; }
Tests:
Spread Operator
a = new Array(10000).fill().map((_, i) => i) a = [0, ...a];
Unshift
a = new Array(10000).fill().map((_, i) => i) a.unshift(0);
Unshift without mutation
a = new Array(10000).fill().map((_, i) => i) a.slice(0).unshift(0);
Prepend function
a = new Array(10000).fill().map((_, i) => i) prependArray(0, a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Spread Operator
Unshift
Unshift without mutation
Prepend function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_3_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.3.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread Operator
28927.7 Ops/sec
Unshift
34056.5 Ops/sec
Unshift without mutation
27897.1 Ops/sec
Prepend function
11977.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, along with the pros and cons of different approaches. **Benchmark Definition** The benchmark definition represents a JavaScript microbenchmark. It defines a small piece of code that will be executed repeatedly to measure its performance. In this case, there are four test cases: 1. `Spread Operator`: The code creates an array of 10,000 elements using `Array(10000).fill().map((_, i) => i)` and then uses the spread operator (`[...a]`) to create a new array with the first element set to 0. 2. `Unshift`: The code creates an array of 10,000 elements using `Array(10000).fill().map((_, i) => i)` and then uses the `unshift` method to add 0 as the first element. 3. `Unshift without mutation`: This test case is similar to the previous one, but it creates a new array instead of modifying the original one using `slice(0)`. 4. `Prepend function`: The code defines a custom function `prependArray` that takes two arguments: `value` and `oldArray`. It returns a new array with the `value` as the first element and the rest of the elements from `oldArray`. **Test Cases** Each test case represents a different approach to adding an element to the beginning of an array. Here's what's being tested: * **Spread Operator**: The spread operator is used to create a new array by taking the elements from the original array and adding a new element at the beginning. + Pros: Fast, efficient way to add elements to the beginning of an array. It creates a new array, which can be beneficial for performance-critical code. + Cons: Creates a new array, which can lead to memory allocation overhead. * **Unshift**: The `unshift` method modifies the original array by adding a new element at the beginning. + Pros: Modifies the original array, which can be beneficial for reducing memory allocation overhead. + Cons: Slower than spread operator due to the need to modify the existing array. * **Unshift without mutation**: This test case creates a new array instead of modifying the original one using `slice(0)`. + Pros: Similar performance benefits as unshift, but avoids modifying the original array. + Cons: Creates an additional copy of the original array. * **Prepend function**: The custom `prependArray` function adds an element to the beginning of the array using a loop. + Pros: Customizable implementation that can be optimized for specific use cases. + Cons: Generally slower than spread operator and unshift due to the need for iteration. **Library** There is no explicit library mentioned in the benchmark definition. However, some test cases may rely on built-in JavaScript methods or functions, such as `Array.prototype.unshift` or `Array.prototype.slice`. **Special JS Feature/Syntax** None of the test cases use any special JavaScript features or syntax beyond what's described above. **Alternatives** Other approaches to adding an element to the beginning of an array could include: * Using `concat` instead of spread operator: `a.concat([0])` * Using `push` and then shifting the elements using `shift`: `a.push(0); a.shift()` * Using a custom implementation with a different data structure, such as a linked list. Keep in mind that these alternatives may have varying performance characteristics depending on the specific use case.
Related benchmarks:
martix-invert
teststest
teststest1
Array splice is slow
Comments
Confirm delete:
Do you really want to delete benchmark?