Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
pop() vs shift() vs unshift vs pop #2
(version: 0)
Comparing performance of:
pop vs shift vs unshift vs push
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for(var i = 0; i < 100000; i++){array.push(Math.random());}
Tests:
pop
while (array.length > 0) { array.pop(); }
shift
while (array.length > 0) { array.shift(); }
unshift
let a = []; for(var i = 0; i < 100000; ++i) { a.unshift(i); }
push
let a = new Array(100000); for(var i = 0; i < 100000; ++i) { a.push(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
pop
shift
unshift
push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pop
153451792.0 Ops/sec
shift
155947184.0 Ops/sec
unshift
2.9 Ops/sec
push
341.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of four different array manipulation operations: 1. `pop()` 2. `shift()` 3. `unshift()` 4. `push()` These operations are commonly used in JavaScript when working with arrays. **Options Compared** The benchmark compares the execution speed of each operation on a large array of 100,000 elements. The size of the array is a common factor in many microbenchmarks, as it allows for a good balance between statistical significance and computational cost. **Pros and Cons of Different Approaches:** 1. **`pop()`**: This operation removes an element from the end of the array and returns it. Its performance can be affected by the size of the array, as it needs to traverse the entire array to find the last element. * Pros: Simple to implement, efficient in terms of memory usage. * Cons: Can be slower for large arrays due to the need to traverse the entire array. 2. **`shift()`**: This operation removes an element from the beginning of the array and returns it. Like `pop()`, its performance can be affected by the size of the array, as it needs to traverse the entire array to find the first element. * Pros: Simple to implement, efficient in terms of memory usage. * Cons: Can be slower for large arrays due to the need to traverse the entire array. 3. **`unshift()`**: This operation adds an element to the beginning of the array and returns a new array with the added element. Its performance can be affected by the size of the array, as it needs to allocate new memory to accommodate the additional element. * Pros: More efficient than `push()` for adding elements to the front of the array. * Cons: Can be slower than `push()` for small arrays due to the allocation overhead. 4. **`push()`**: This operation adds an element to the end of the array and returns a new array with the added element. Its performance can be affected by the size of the array, as it needs to allocate new memory to accommodate the additional element. * Pros: More efficient than `unshift()` for adding elements to the back of the array. * Cons: Can be slower than `unshift()` for small arrays due to the allocation overhead. **Library and Special JS Feature** The benchmark uses JavaScript as the programming language, and it does not specify any additional libraries or special features. However, it's worth noting that some modern browsers may have optimizations or features that can affect the performance of these operations. **Other Alternatives** Some alternative approaches to measuring array manipulation performance include: * Using a different data structure, such as a linked list or a vector * Measuring performance on smaller arrays with varying sizes * Adding additional factors to the benchmark, such as sorting or searching the array * Using a different programming language or framework It's worth noting that these alternative approaches may not accurately represent real-world use cases, and the results of this benchmark should be taken as an approximation rather than a definitive measure of performance.
Related benchmarks:
Already sorted versus random
Array.Sort vs Math.Min-Max
Fisher-Yates Shuffle
set.has vs. array.includes vs obj[key] vs map.get 2
Set.has v.s Array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?