Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arr unshift vs push + reverse (recreating size 50 array)
(version: 0)
Comparing performance of:
unshift vs push + reverse
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
unshift
var arr = []; for (let i = 0; i < 50; i++){ arr.unshift(i); }
push + reverse
var arr = []; for (let i = 0; i < 50; i++){ arr.push(i); } arr.reverse();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unshift
push + reverse
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unshift
923963.8 Ops/sec
push + reverse
10526558.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the MeasureThat.net benchmark. **Benchmark Overview** The benchmark measures the performance difference between two approaches: using `arr.unshift()` to add elements to an array, versus using `push()` followed by reversing the array. **Options Compared** There are only two options being compared in this benchmark: 1. **`unshift()`**: adds elements to the beginning of the array. 2. **`push()` + `reverse()`**: adds elements to the end of the array and then reverses it. **Pros and Cons of Each Approach** * **`unshift()`**: + Pros: can be faster for certain scenarios (e.g., when adding elements at the beginning of an array), as it avoids the overhead of reversing the array. + Cons: can be slower than `push()` + `reverse()` in general, since it requires shifting all existing elements to make space at the beginning of the array. * **`push()` + `reverse()`**: + Pros: is generally faster and more memory-efficient, as it adds elements to the end of the array without requiring any shifts or reversals. + Cons: requires two operations (adding elements and reversing), which can be slower than a single operation like `unshift()`, especially for small arrays. **Library** There is no explicit library mentioned in the benchmark definition. However, it's likely that the standard JavaScript built-in methods (`arr.unshift()` and `arr.push()`) are being used. **Special JS Features or Syntax** There are no special features or syntax explicitly mentioned in this benchmark. The code snippets provided only use basic JavaScript constructs like loops, arrays, and function declarations. **Other Alternatives** For comparison, other approaches to adding elements to an array could include: * Using `splice()` instead of `push()`: `arr.splice(0, 0, i);` * Using a custom implementation using indexed assignment: `arr[i] = i;` (not shown in the benchmark) * Using a library or framework-specific method (if applicable) Keep in mind that the performance differences between these alternatives may vary depending on the specific use case and requirements. Overall, this benchmark provides a simple yet insightful comparison of two common approaches to adding elements to an array in JavaScript.
Related benchmarks:
arr unshift vs push + reverse (large array)
arr unshift vs push + reverse (big array)
myarr unshift vs push + reverse (small array)2
arr unshift vs push + reverse (small array) one item
arr unshift vs push + reverse (size 50 array)
Comments
Confirm delete:
Do you really want to delete benchmark?