Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
pop() vs shift() vs unshift vs pop vs index
(version: 0)
Comparing performance of:
pop vs shift vs unshift vs push vs index
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100000); for(let i = 0; i < 100000; ++i) { array[i] = i; }
Tests:
pop
while (array.length > 0) { array.pop(); }
shift
while (array.length > 0) { array.shift(); }
unshift
let a = []; for(let i = 0; i < 100000; ++i) { a.unshift(i); }
push
let a = []; for(let i = 0; i < 100000; ++i) { a.push(i); }
index
let a = new Array(100000); for(let i = 0; i < 100000; ++i) { a[i] = i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
pop
shift
unshift
push
index
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pop
15367090.0 Ops/sec
shift
15580063.0 Ops/sec
unshift
6.2 Ops/sec
push
2106.2 Ops/sec
index
6414.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test for JavaScript microbenchmarks on the MeasureThat.net website. The test compares the performance of different array manipulation methods: 1. **pop()**: Removes and returns the last element from an array. 2. **shift()**: Removes and returns the first element from an array. 3. **unshift()**: Adds one or more elements to the beginning of an array. 4. **push()**: Adds one or more elements to the end of an array. 5. **index** (Note: This is not a standard JavaScript method for accessing array elements, but rather likely a custom implementation for this benchmark test): accesses an element at its specified index. Here's a brief overview of each approach, their pros and cons: 1. **pop()**: Pros: * Simple to implement. * Fast because it only needs to traverse the array from the end to the beginning to find the last element. Cons: * May not be suitable for large arrays or arrays with many duplicates (since `pop()` will always return the last element, regardless of its position). 2. **shift()**: Pros: * Similar to `pop()`, but removes and returns the first element instead. Cons: * May have similar performance issues as `pop()` if the array has many duplicates or is very large. 3. **unshift()**: Pros: * Adds elements to the beginning of the array, which can be faster than appending elements to the end using `push()`. Cons: * Can be slower for arrays with a large number of elements at the end (due to the need to shift all subsequent elements). 4. **push()**: Pros: * Appends elements to the end of the array, which is often faster than inserting elements at the beginning using `unshift()`. Cons: * May have performance issues if the array is very large or has many duplicates (since the browser needs to traverse the entire array). 5. **index** (Note: This is likely a custom implementation for this benchmark test): The specifics of this approach are not clear, but it's possible that it's optimized for certain use cases. The provided benchmark results show that: * `pop()` and `shift()` have similar performance across different browsers and devices. * `push()` has lower performance compared to `pop()` and `shift()`. * `unshift()` has the lowest performance of all, possibly due to its need to shift elements at the beginning of the array. Keep in mind that these results may vary depending on the specific use case, browser, and device being tested. Other alternatives for this benchmark test could include: 1. **splice()**: A method that allows removing or inserting elements at specific positions in an array. 2. **array.prototype.map()** / **array.prototype.forEach()**: Methods that can be used to create new arrays with transformed data. 3. **array.prototype.slice()**: A method that creates a shallow copy of a portion of an array. However, these alternatives may have different performance characteristics and might not provide the same level of insight as the original benchmark test.
Related benchmarks:
Splice vs Spread vs Unshift to insert at beginning of array (fixed from slice)
Splice vs shift to remove at beginning of array (fixed from slice)
Splice vs Shift to remove from the beginning
Array.from() vs new Array() vs push
Empty array: Splice vs Shift vs Pop
Comments
Confirm delete:
Do you really want to delete benchmark?