Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.push vs Array.prototype.shift
(version: 0)
Compare both Array methods
Comparing performance of:
Array.prototype.push vs Array.prototype.unshift
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [0, 1, 2, 3, 4];
Tests:
Array.prototype.push
array.push(5);
Array.prototype.unshift
array.unshift(5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.push
Array.prototype.unshift
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.push
22870924.0 Ops/sec
Array.prototype.unshift
57.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is tested on the provided JSON?** The benchmark tests two different ways to add an element to the end of an array in JavaScript: `Array.prototype.push` and `Array.prototype.unshift`. These methods are part of the Array prototype, which means they're built-in functions that can be called on any array object. **Options compared** There are two options being compared: 1. **`Array.prototype.push`**: This method adds one or more elements to the end of an array and returns the new length of the array. 2. **`Array.prototype.unshift`**: This method adds one or more elements to the beginning of an array and returns the new length of the array. **Pros and cons of each approach** * `Array.prototype.push`: + Pros: - More efficient for adding elements to the end of an array, as it only needs to update the last element's index. - Less memory overhead, as it doesn't need to shift existing elements. + Cons: - Can be slower than `unshift` for small arrays, since it needs to calculate the new length and update each element's index. * `Array.prototype.unshift`: + Pros: - Faster than `push` for small arrays, since it can take advantage of existing gaps in the array and only need to shift elements that need to be moved. + Cons: - More memory overhead, as it needs to create a temporary buffer to hold the new elements while they're being shifted into place. - Slower than `push` for large arrays, since it needs to update each element's index. **Library and purpose** The benchmark doesn't use any external libraries. However, it does use built-in JavaScript features like Array.prototype.push and Array.prototype.unshift. **Special JS feature or syntax** There is no special JS feature or syntax being used in this benchmark. It only uses standard JavaScript language constructs. **Other alternatives** If you want to test other array methods, such as `Array.prototype.splice`, `Array.prototype.forEach`, or `Array.prototype.map`, you can add additional benchmark definitions to the JSON object. Here's an example of how you could add another benchmark definition: ``` { "Name": "Array.prototype.splice", "Description": "Compare Array.prototype.splice with push and unshift", ... } ``` You can also experiment with different array sizes, or use a larger array to test the performance difference between `push` and `unshift`. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Array spread vs. push performance
Array.prototype.push vs Array.prototype.push.apply
Array.prototype.push vs Array.prototype.push.apply2
Array Push vs. Index Access
Comments
Confirm delete:
Do you really want to delete benchmark?