Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arr unshift vs push + reverse (big array)
(version: 0)
Comparing performance of:
unshift vs push + reverse
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [10];
Tests:
unshift
for (let i = 0; i < 100000; i++){ arr.unshift(i); }
push + reverse
for (let i = 0; i < 100000; 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unshift
0.2 Ops/sec
push + reverse
60.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark measures the performance of two JavaScript operations on large arrays: `unshift` and `push + reverse`. The test cases create a small initial array with 10 elements and then either use `unshift` to add 100,000 elements from scratch or use `push` followed by `reverse` to add the same amount of elements in reverse order. **Approaches Compared** 1. **`unshift`**: This operation adds an element to the end of the array without shifting existing elements. It's a relatively simple and efficient operation. 2. **`push + reverse`**: This approach uses `push` to add elements to the end of the array, followed by `reverse` to rearrange the elements in reverse order. **Pros and Cons** * **`unshift`**: + Pros: Simple, fast, and efficient ( O(1) average case). + Cons: May not be suitable for arrays with many existing elements, as it can lead to fragmentation. * **`push + reverse`**: + Pros: Can handle large arrays with many existing elements, as `push` doesn't affect the indexing of existing elements. Also, `reverse` can take advantage of JavaScript's caching mechanisms for reversals. + Cons: More complex and slower than `unshift`, especially for small to medium-sized arrays. **Other Considerations** * **Array size**: The benchmark creates a large array with 100,000 elements. This is likely done to minimize the impact of array resizing or reallocation, which can be expensive operations. * **JavaScript engine**: The benchmark is run on Chrome 123, which may not be the most recent version. Additionally, the `RawUAString` field suggests that the browser's User Agent string is generated by a custom test harness. * **Device and OS**: The benchmark is run on a desktop with Mac OS X 10.15.7. **Library/Dependencies** None of the provided code snippets use any external libraries or dependencies, other than the built-in JavaScript `Array` methods. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark beyond the standard array operations and conditional statements (e.g., `for`, `let`, `const`).
Related benchmarks:
arr unshift vs push + reverse (small array)
arr unshift vs push + reverse (large array)
myarr unshift vs push + reverse (small array)
arr unshift vs push + reverse (small array) one item
Comments
Confirm delete:
Do you really want to delete benchmark?