Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array .pop() vs .shift()
(version: 0)
Comparing performance of:
.shift() vs .pop()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []
Tests:
.shift()
arr.push(42) arr.shift()
.pop()
arr.push(42) arr.pop()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.shift()
.pop()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.shift()
40684128.0 Ops/sec
.pop()
89107480.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark is called "Array .pop() vs .shift()" and it tests two different approaches to removing an element from the end of an array in JavaScript: using `.pop()` and using `.shift()`. **Options Compared** The options being compared are: 1. `arr.push(42)\r\narr.shift()`: This approach pushes a new element onto the array before shifting the last element. 2. `arr.push(42)\r\narr.pop()`: This approach also pushes a new element onto the array, but then immediately removes the last element using `.pop()`. **Pros and Cons** The pros of each approach are: * `.push()` followed by `.shift()` is generally faster because it avoids the overhead of creating a temporary array or buffer to store the popped element. * `.push()` followed by `.pop()` might be slower because it creates a temporary array or buffer, which can be expensive. However, there's an important consideration: both approaches are relatively inefficient and should be avoided in real-world code whenever possible. In general, it's better to use `array.prototype.slice().reverse().shift()` or `array.splice(array.length - 1)` for removing elements from the end of an array. **Library** The provided benchmark script doesn't explicitly require any external libraries, but it does create a new empty array `var arr = []`. This is a simple and common pattern in JavaScript testing to create an empty array that can be populated with test data. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. It's written in vanilla JavaScript. **Alternatives** If you're interested in creating your own benchmarks, here are some alternatives: 1. MeasureThat.net: This is the tool being used to create and run the benchmark. 2. Chrome DevTools: You can use the Chrome DevTools to run your own custom benchmarks using the `Benchmark` API. 3. JavaScriptPerf.com: This is another website that allows you to create and share JavaScript benchmarks. In conclusion, this benchmark tests two different approaches to removing an element from the end of an array in JavaScript: using `.pop()` and using `.shift()`. While there are pros and cons to each approach, neither is recommended for use in real-world code.
Related benchmarks:
arr.pop() vs arr.shift() vs arr[0]
Array Slice vs Pop
Slice vs Pop and Shift
Array slice vs shift (2)
Comments
Confirm delete:
Do you really want to delete benchmark?