Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs unshift
(version: 0)
push vs unshift
Comparing performance of:
arrayPush-1 vs arrayUnshift-1 vs arrayConact-1
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [];
Tests:
arrayPush-1
arr.push(1)
arrayUnshift-1
arr.unshift(1)
arrayConact-1
arr = [1, ...arr]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
arrayPush-1
arrayUnshift-1
arrayConact-1
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):
I'd be happy to help you understand the JavaScript microbenchmarking test case on MeasureThat.net. **Benchmark Overview** The benchmark compares three different approaches for adding an element to an array in JavaScript: 1. `arr.push(1)` 2. `arr.unshift(1)` 3. `arr = [1, ...arr]` **Approaches Compared** Here's a brief description of each approach: * **`arr.push(1)`**: This method adds the specified value to the end of the array. * **`arr.unshift(1)`**: This method adds the specified value to the beginning of the array. * **`arr = [1, ...arr]`**: This is a more modern approach using spread syntax. It creates a new array by concatenating the original array with the element `1`. **Pros and Cons** Here are some pros and cons for each approach: * **`arr.push(1)`**: + Pros: Simple, widely supported, efficient. + Cons: Can be slower than other methods in some cases due to array resizing. * **`arr.unshift(1)`**: + Pros: Fastest method among the three, suitable for adding elements to the beginning of an array. + Cons: Requires more memory allocation and copying when used with large arrays. * **`arr = [1, ...arr]`**: + Pros: Modern syntax, concise, and efficient for adding a single element. + Cons: Can be slower than other methods for larger arrays due to the creation of a new array. **Library Used** None. This benchmark uses only built-in JavaScript features. **Special JS Feature/Syntax** The `...` spread operator used in the third approach (`arr = [1, ...arr]`) is a modern syntax introduced in ECMAScript 2015 (ES6). It's supported by most modern browsers and Node.js versions. **Other Alternatives** If you want to test other array operations or different algorithms, MeasureThat.net provides many benchmarks for various JavaScript scenarios. Some examples include: * Array concatenation: `arr.concat([1])` * Array slicing: `arr.slice(0, 10)` * Array sorting and searching * Object creation and comparison Please note that the benchmark results may vary depending on the specific browser, device platform, operating system, and other factors.
Related benchmarks:
Array .push() vs .unshift()
Array .push() vs .unshift() + ref to last
Array .push() vs .unshift() vs spread
Array .push() vs .unshift() multiple
Array .push() vs .unshift() |
Comments
Confirm delete:
Do you really want to delete benchmark?