Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array .push() vs .unshift()
(version: 0)
Comparing performance of:
.unshift() vs .push()
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []
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:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.unshift()
656855.9 Ops/sec
.push()
20894864.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark and its components. **What is tested:** The provided JSON represents a JavaScript microbenchmark that compares the performance of two approaches: `arr.push()` and `arr.unshift()`. The test creates an empty array, `var arr = []`, and then pushes or unshifts a value (42) into it. The benchmark measures the time taken to execute these operations. **Options compared:** Two options are being compared: 1. **`arr.push(42)`**: This approach adds the value 42 to the end of the array using the `push()` method. 2. **`arr.unshift(42)`**: This approach inserts the value 42 at the beginning of the array using the `unshift()` method. **Pros and cons:** 1. **`arr.push(42)`**: * Pros: More common and efficient way to add an element to the end of an array. * Cons: May require more memory allocations if the array is large, leading to slower performance due to garbage collection. 2. **`arr.unshift(42)`**: * Pros: Can be faster than `push()` for small arrays or when inserting at the beginning is necessary (e.g., sorting). * Cons: Requires more memory allocations and can lead to slower performance due to garbage collection, especially for large arrays. **Library and purpose:** The test uses no specific JavaScript library. It's a basic example of a microbenchmarking test that relies on the built-in JavaScript methods `push()` and `unshift()`. **Special JS feature or syntax:** There is no special JavaScript feature or syntax mentioned in this benchmark. The code only uses standard JavaScript constructs. **Other alternatives:** For benchmarking JavaScript performance, other approaches include: 1. **BenchmarkDotNet**: A popular benchmarking library for Node.js and browser environments. 2. **jsPerf**: A simple benchmarking tool that comes bundled with the Chromium codebase. 3. **Google Benchmark**: A high-performance benchmarking framework developed by Google. When choosing a benchmarking approach, consider factors like ease of use, customization options, and compatibility with your target environment (e.g., Node.js, browser).
Related benchmarks:
Array .push() vs .unshift()
Array .push() vs .unshift() + ref to last
Array .push() vs .unshift() multiple
Array .push() vs .unshift() |
Comments
Confirm delete:
Do you really want to delete benchmark?