Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arr unshift vs push + reverse vs concat case(small array)
(version: 0)
Comparing performance of:
unshift vs push + reverse vs concat
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [10];
Tests:
unshift
for (let i = 0; i < 10; i++){ arr.unshift(i); }
push + reverse
for (let i = 0; i < 10; i++){ arr.push(i); } arr.reverse();
concat
for (let i = 0; i < 10; i++){ arr = [i].concat(arr) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
unshift
push + reverse
concat
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):
**What is being tested?** The provided benchmark measures the performance of three different approaches for modifying an array in JavaScript: 1. `unshift()`: Adds one or more elements to the beginning of an array. 2. `push()` followed by `reverse()`: Adds one or more elements to the end of an array and then reverses the order of its elements. 3. `concat()`: Merges two or more arrays together. **Options compared:** These three approaches are being compared in terms of their performance, specifically: * The number of executions per second (in this case, measured for Chrome 109 on a Mac OS X 10.15.7 desktop). * The raw UA string and other device information is not relevant to the comparison of these specific methods. **Pros and Cons:** 1. `unshift()`: * Pros: Can be faster when adding elements to the beginning of an array, as it only needs to update the index of each element. * Cons: Requires a new array to be created if the original array is empty, which can lead to increased memory allocation and deallocation overhead. 2. `push()` followed by `reverse()`: * Pros: Allows for efficient addition of elements to the end of an array without creating a new array, and then reverses the order without needing to update indices. * Cons: Requires reversing the entire array after pushing new elements, which can be slower than other approaches if the array is large. 3. `concat()`: * Pros: Merges two arrays without modifying either one, making it easier to combine data from multiple sources. * Cons: Creates a new array and copies all elements from both input arrays, which can lead to increased memory allocation and deallocation overhead. **Library usage:** None of the benchmarked code snippets use any external libraries. However, Chrome's JavaScript engine may have some internal optimizations or features that could affect performance. **Special JS feature or syntax:** The benchmark uses ES6 `let` and `const` variables (e.g., `var arr = [10];`) and JavaScript arrays (`arr.unshift(i)`). **Other alternatives:** Additional methods for modifying an array in JavaScript include: * Using `splice()` to remove and replace elements. * Using `forEach()` or other array methods to iterate over the array and modify its elements. * Using `Array.prototype.map()`, `Array.prototype.filter()`, or other array methods to create new arrays without modifying the original one. These alternatives may have different performance characteristics depending on the specific use case, and may not be as optimized as the `unshift()`, `push()` + `reverse()`, and `concat()` approaches.
Related benchmarks:
Array spread vs. push performance
arr unshift vs push + reverse (small array)
myarr unshift vs push + reverse (small array)
myarr unshift vs push + reverse (small array)2
arr unshift vs push + reverse (small array) one item
Comments
Confirm delete:
Do you really want to delete benchmark?