Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array .push() vs .unshift() multiple
(version: 0)
Comparing performance of:
.unshift() vs .push()
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from(Array(10000), (val, index) => index);
Tests:
.unshift()
arr.unshift(42)
.push()
arr.push(42)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.unshift()
.push()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.unshift()
39231.8 Ops/sec
.push()
11287872.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is tested?** The provided benchmark tests two different methods for adding elements to an array: `arr.unshift(42)` and `arr.push(42)`. The goal is to compare their performance in a controlled environment. **Options compared:** There are only two options being compared: 1. **`arr.unshift(42)`**: This method adds the element at the beginning of the array. 2. **`arr.push(42)`**: This method adds the element at the end of the array. **Pros and Cons:** * **`arr.unshift(42)`**: This method is generally faster than `push()` because it only needs to shift elements from the current end of the array, reducing the number of operations required. However, it modifies the array in place, which might be less efficient for large arrays if the array needs to be preserved. * **`arr.push(42)`**: This method is often considered more intuitive and easier to use than `unshift()`, especially when adding elements at the end of an array. It's also a good choice when you want to preserve the original array. **Library and syntax:** There is no external library used in this benchmark, so it relies solely on built-in JavaScript functionality. There are no special JS features or syntax being tested in this case. **Alternative approaches:** Other alternatives for adding elements to an array might include: * **Using `Array.prototype.concat()`**: You could use `concat()` with the element you want to add as an argument, like so: `arr = arr.concat([42])`. However, this method is generally slower and more memory-intensive than `push()`. * **Using `Array.prototype.splice()`**: This method would also work for adding elements at the end of the array, but it's typically less efficient due to its use of multiple operations (insertion at a specific index). * **Using other data structures**: Depending on your needs, you might consider using data structures like linked lists or stacks instead of arrays. **Benchmark preparation code:** The provided `Script Preparation Code` is used to create an array with 10,000 elements, each initialized with its index value. This allows for a fair comparison between the different methods being tested. Here's how the script works: ```javascript var arr = Array.from(Array(10000), (val, index) => index); ``` In this code snippet: * `Array.from()` is used to create an array with elements initialized by a callback function. * `(val, index) => index` defines the value for each element in the new array. The callback function receives two parameters: the current element from the outer array (`val`) and its index in that array (`index`).
Related benchmarks:
Array .push() vs .unshift() + ref to last
Array .push() vs .unshift(), 1M elements
Array .push() vs .unshift() vs spread
Array .push() vs .unshift() |
Comments
Confirm delete:
Do you really want to delete benchmark?