Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array .reverse() .push() vs .unshift()
(version: 0)
Comparing performance of:
.unshift() vs .push()
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1]
Tests:
.unshift()
for (let index = 0; index < 100; index++) arr.unshift(0)
.push()
arr.reverse() for (let index = 0; index < 100; index++) arr.push(0) arr.reverse()
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of two different approaches to add elements to an array in JavaScript: using the `.unshift()` method versus using the `.push()` method followed by reversing the array. The goal is to determine which approach is faster. **Options Compared** Two options are compared: 1. **.unshift()**: This method adds one or more elements to the beginning of an array. 2. **.push() + .reverse()**: This approach involves adding elements to the end of the array using `.push()` and then reversing the array using `.reverse()`. **Pros and Cons** * **.unshift()**: + Pros: Can add elements in O(1) time complexity, efficient for sparse arrays. + Cons: May not be as efficient for dense arrays (e.g., arrays with many consecutive elements), since it requires shifting all existing elements to make room for the new one. * **.push() + .reverse()**: + Pros: Can be more efficient for dense arrays, since reversing the array is an O(n) operation that only needs to be done once. + Cons: Requires two separate operations (adding elements and reversing), which can lead to overhead. **Library** There is no explicit library mentioned in the benchmark definition or test cases. However, the use of JavaScript syntax suggests that it's a browser-based benchmark, likely targeting modern web browsers. **Special JS Feature/Syntax** The benchmark uses the `for...of` loop and `let` keyword for variable declaration, which are part of ECMAScript 2015 (ES6) syntax. The `let index = 0;` line declares a new variable `index` with `let`, while `for (let index = 0; index < 100; index++)` is the loop body. **Other Alternatives** If you wanted to compare different approaches to add elements to an array, some alternative methods could be: * Using `.concat()` instead of `.push()` * Using a data structure other than arrays, like linked lists or sets * Implementing custom insertion algorithms, like insertion sort Keep in mind that the performance differences between these approaches can depend on various factors, such as the size and distribution of the elements being added. **Benchmark Preparation Code** The provided script preparation code creates an array `arr` with a single element: `var arr = [1]`. This initializes the array for testing. **Individual Test Cases** Each test case has a unique benchmark definition and test name: * **.unshift()**: Tests adding 100 elements to the beginning of the array using `.unshift()`. * **.push() + .reverse()**: Tests adding 100 elements to the end of the array using `.push()` followed by reversing the array with `.reverse()`. **Latest Benchmark Result** The provided result shows the performance metrics for each test case: * **.unshift()**: The browser executed approximately 5311 times per second. * **.push() + .reverse()**: The browser executed approximately 71 times per second.
Related benchmarks:
arr unshift vs push + reverse (small array)
myarr unshift vs push + reverse (small array)
arr unshift vs push + reverse (small array) one item
arr unshift vs push + reverse (mid array)
arr unshift vs push + reverse (size 50 array)
Comments
Confirm delete:
Do you really want to delete benchmark?