Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs concat vs unshift correct
(version: 0)
spread vs concat vs unshift
Comparing performance of:
arrayUnshift123 vs arrayConcat123 vs arraySpread123
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [1,2,3];
Tests:
arrayUnshift123
array.unshift(0);
arrayConcat123
array = [0].concat(array)
arraySpread123
array = [0, ...array]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
arrayUnshift123
arrayConcat123
arraySpread123
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Definition** The benchmark definition specifies that three different methods are being compared: 1. `unshift(0)`: This method adds an element to the end of an array without modifying its length. 2. `[...array]` (spread syntax): This method creates a new array by spreading the existing array into a new array. 3. `[0].concat(array)` : This method concatenates two arrays, where one of them is created by taking the first element of another array and combining it with the original array. **What are being compared?** The benchmark compares the performance (measured in executions per second) of these three methods on a sample JavaScript array `[1, 2, 3]`. **Options Comparison** * `unshift(0)` vs `[...array]` vs `[0].concat(array)` + **Pros and Cons:** - `unshift(0)`: Simple and efficient for small arrays. However, it can be slow for large arrays because it needs to update the array's length. - `[...array]`: Creates a new array, which is more memory-efficient but slower than modifying the original array. - `[0].concat(array)`: Concatenates two arrays, which is slower and less efficient than modifying the original array. * **Other Considerations:** + The benchmark does not consider other methods like `push(0)` or using `Array.prototype.push()` with an initial value. **Library Usage** None of the test cases use any external libraries. **Special JS Features/Syntax** The test case uses the spread syntax `[...array]`, which is a relatively recent feature introduced in ECMAScript 2015 (ES6). **Benchmark Preparation Code and HTML Preparation Code** The benchmark preparation code creates an array `var array = [1,2,3];`. The HTML preparation code is empty (`null`), indicating that the test does not require any HTML setup. **Other Alternatives** If you wanted to add more tests or variations, you could consider: * Using other methods for adding elements to the end of an array (e.g., `push(0)`, `Array.prototype.push()` with an initial value) * Comparing performance with different array lengths or distributions * Testing other array manipulation methods, such as `slice()`, `splice()`, or `map()` * Using different browsers or devices to test compatibility and variability in performance
Related benchmarks:
concat vs unshift vs spread
unshift vs spread vs concat
spread vs concat vs unshift22
Array.prototype.concat vs spread operator (new try)
Comments
Confirm delete:
Do you really want to delete benchmark?