Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Push&assign vs Unshift
(version: 1)
Comparing performance of:
Push vs Unshift
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr1; var arr2;
Tests:
Push
arr1 = [1, 2, 3]; arr2 = [4, 5, 6]; arr1.push(...arr2); arr2 = arr1;
Unshift
arr1 = [1, 2, 3]; arr2 = [4, 5, 6]; arr2.unshift(...arr1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Push
Unshift
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 OPR/113.0.0.0
Browser/OS:
Opera 113 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Push
2692897.2 Ops/sec
Unshift
3230785.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the JavaScript microbenchmark provided by MeasureThat.net. **Benchmark Overview** The benchmark tests two approaches for array manipulation: `push` and `unshift`. Both approaches involve creating an initial array (`arr1`) and another array (`arr2`) with some values. The test then measures the performance of each approach to perform one of two operations: 1. Assigning `arr2` to `arr1`, followed by pushing all elements of `arr2` onto `arr1`. 2. Prepending all elements of `arr1` to `arr2`. **Options Compared** Two options are compared in this benchmark: 1. **Push**: This approach uses the `push()` method to add elements to the end of an array. 2. **Unshift**: This approach uses the `unshift()` method to add elements to the beginning of an array. **Pros and Cons of Each Approach** * **Push**: + Pros: Generally faster, as it only needs to iterate over the elements to be pushed, without the overhead of inserting at a specific position. + Cons: Requires iterating over the elements to be pushed, which can lead to slower performance for large arrays if `push()` is not optimized. * **Unshift**: + Pros: More efficient when pushing multiple elements from the beginning of an array, as it avoids the overhead of inserting at a specific position and only needs to update the length of the array. + Cons: Requires updating the length of the array, which can be slower than `push()` for large arrays. Additionally, it may require more memory allocation. **Library and Purpose** There is no explicit library mentioned in the benchmark definition. However, `push()` and `unshift()` are built-in JavaScript methods that operate on arrays. **Special JS Feature or Syntax (None)** There are no special features or syntax used in this benchmark. **Other Alternatives** If you need to perform similar operations, other alternatives could be: * Using a library like Lodash, which provides a `concat()` method for concatenating arrays. * Using the spread operator (`...`) with `push()` and `unshift()`, e.g., `arr1.push(...arr2)` or `arr1.unshift(...arr2)`. * Using `splice()` to insert elements at specific positions in an array. Keep in mind that these alternatives may have different performance characteristics, depending on the specific use case and the implementation of the library or method used.
Related benchmarks:
Array .push() vs .unshift()
Array .push() vs .unshift() + ref to last
Array .push() vs .unshift() vs spread
Array .push() vs .unshift() multiple
Array .push() vs .unshift() |
Comments
Confirm delete:
Do you really want to delete benchmark?