Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs concat vs unshift on 1000
(version: 0)
spread vs concat vs unshift
Comparing performance of:
Unshift vs Concat vs Spread
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var array = new Array(1000).fill(1);
Tests:
Unshift
array.unshift(0);
Concat
array = [0].concat(array)
Spread
array = [0, ...array]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Unshift
Concat
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Unshift
22K/s
Concat
591/s
Spread
64/s
View exact numbers
Test name
Executions per second
✓
Unshift
21,505 Ops/sec
Concat
591 Ops/sec
Spread
64 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Main Test Case: Spread vs Concat vs Unshift on 1000** The main test case is comparing three different approaches to add an element at the beginning of an array: 1. `array.unshift(0);` (Unshift) 2. `array = [0].concat(array);` (Concat) 3. `array = [0, ...array];` (Spread) **Options Compared** The test case is comparing these three approaches in terms of performance. Specifically, it's measuring the time taken to execute each operation. **Pros and Cons of Each Approach** 1. **Unshift**: This approach adds an element at the beginning of the array by incrementing the length of the array and then pushing the new element into the array. * Pros: Can be more efficient than concat for large arrays since it avoids creating a new array, but still involves pushing elements to the end of the array. * Cons: May incur additional overhead due to the push operation. 2. **Concat**: This approach creates a new array by concatenating the original array with an array containing the new element. * Pros: Creates a new array, which can be beneficial for avoiding side effects in some cases. * Cons: Involves creating a new array, which can be memory-intensive and slower than pushing to the end of an existing array. 3. **Spread**: This approach uses the spread operator (`...`) to add an element at the beginning of the array. * Pros: Can be more efficient than concat since it avoids creating a new array and only adds the new element to the beginning. * Cons: May incur additional overhead due to the creation of a new array. **Library Used** There is no library explicitly mentioned in the provided JSON. However, some modern JavaScript engines may have built-in optimizations or features that can affect performance, but these are not typically considered external libraries. **Special JS Feature/Syntax** The `...` operator used in the Spread approach is called the spread operator or the rest operator. It was introduced in ECMAScript 2015 (ES6) as a part of the standard library. The spread operator allows for spreading the elements of an array into another array or object. **Other Alternatives** Some alternative approaches to adding an element at the beginning of an array include: * Using `Array.prototype.set()` method, which is not supported in older browsers. * Using `Array.prototype.push()` followed by assigning the result back to the original array, which may incur additional overhead. * Using a library like Lodash's `initial()` function, which provides a more concise and readable way to add an element at the beginning of an array. Keep in mind that these alternatives might not be as efficient or standardized as the three approaches being compared in this test case.
Comments
Confirm delete:
Do you really want to delete benchmark?