Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unshift vs push array
(version: 0)
Comparing performance of:
unshift vs push
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var itemsArray = []; for (let i = 0; i < 100000; i++) { itemsArray.push(i) }
Tests:
unshift
console.log('unshift ', itemsArray.unshift('olololol'))
push
console.log('push ', itemsArray.push('olololol'))
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:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
unshift
8882.9 Ops/sec
push
239190.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's being tested in the provided benchmark. **Overview** The benchmark compares the performance of two methods for adding an element to the end of an array in JavaScript: `unshift()` and `push()`. The test case is designed to measure which method is faster on a specific hardware configuration, in this case, Chrome 122 on a Linux desktop. **Benchmark Definition JSON** The benchmark definition JSON contains the following information: * `Name`: The name of the benchmark, which is "unshift vs push array". * `Script Preparation Code`: A JavaScript code snippet that creates an empty array `itemsArray` and populates it with 100,000 elements using a `for` loop. This code is executed before each test case to ensure consistent results. * `Html Preparation Code`: An empty string, indicating no HTML preparation is required. **Test Cases** The benchmark consists of two individual test cases: 1. **unshift**: Measures the performance of calling `unshift()` on the `itemsArray` with the string "olololol". 2. **push**: Measures the performance of calling `push()` on the `itemsArray` with the string "olololol". **Options Compared** The two options being compared are: * `unshift()`: Adds an element to the beginning of the array. * `push()`: Adds an element to the end of the array. **Pros and Cons** * **Pros of using unshift():** + Can be faster for arrays that need to maintain a specific order (e.g., sorting). + Can reduce memory fragmentation by adding elements to the beginning of the array. * **Cons of using unshift():** + Can lead to slower performance when inserting or removing elements from the middle or end of the array, as it requires shifting all subsequent elements. + May increase memory usage due to the need to allocate new memory for the added element at the beginning of the array. * **Pros of using push():** + Is generally faster and more efficient than `unshift()` when inserting or removing elements from the middle or end of the array, as it only requires updating a single index. + Reduces memory fragmentation by allocating new memory for each added element at the end of the array. * **Cons of using push():** + May lead to slower performance when adding an element to the beginning of the array (i.e., `unshift()`). + Can increase memory usage due to the allocation of new memory for each added element. **Library** There is no library mentioned in the benchmark definition, but it's likely that the `Array.prototype` methods (`unshift()` and `push()`) are being used. **Special JS Feature or Syntax** None are explicitly mentioned, but note that JavaScript has some features like `let`, `const`, and arrow functions (e.g., `() => { ... }`) that can affect performance. However, these are not relevant to this specific benchmark. **Alternatives** Other alternatives for adding elements to the end of an array in JavaScript include: * Using `Array.prototype.concat()` or other methods that create a new array. * Using `splice()` with the `-1` index (which inserts at the end). * Implementing a custom solution using loops and indexing. Keep in mind that these alternatives may have different performance characteristics and use cases, depending on the specific requirements of your application.
Related benchmarks:
Array construct vs array push
Pushing items via Array.push vs. Spread Operator
Splice vs Spread vs Unshift vs Push to insert at beginning of array
Array.from() vs new Array() vs push
Array.from() vs new Array() vs push pushup
Comments
Confirm delete:
Do you really want to delete benchmark?