Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Unshift vs concat
(version: 0)
Benchmark.js
We can add elements to the beginning of array using both, let's find out which is faster. Unshift also mutates the array. Concat does not, so that would be preferable.
Comparing performance of:
Unshift vs Concat
Created:
4 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var arr = [1, 3, 5, 11, 13];
Tests:
Unshift
arr.unshift(4,5)
Concat
[4,5].concat(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Unshift
Concat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:152.0) Gecko/20100101 Firefox/152.0
Browser/OS:
Firefox 152 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Unshift
419K/s
Concat
157/s
View exact numbers
Test name
Executions per second
✓
Unshift
418,842 Ops/sec
Concat
157 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the provided benchmark. **What is being tested?** The benchmark measures the performance difference between two ways of adding elements to the beginning of an array in JavaScript: 1. **Unshift**: This method adds one or more elements to the beginning of an array and returns the new length of the array. 2. **Concat**: This method creates a new array by concatenating the original array with another array, effectively achieving the same result as `unshift`. **Options compared** The benchmark compares two options: 1. **Unshift**: This approach modifies the original array in-place. 2. **Concat**: This approach creates a new array without modifying the original one. **Pros and Cons of each approach:** * **Unshift**: + Pros: Faster, as it doesn't create a new array, and can be more efficient for large arrays. + Cons: Modifies the original array, which might not be desirable in some cases (e.g., when working with immutable data structures). * **Concat**: + Pros: Non-mutating, meaning it doesn't change the original array. This can be beneficial for preserving the original data's integrity. + Cons: Slower, as it creates a new array, which can lead to higher memory usage and slower performance. **Other considerations** When choosing between `unshift` and `concat`, consider the following: * **Memory usage**: If you're working with large arrays or have limited memory resources, using `unshift` might be more efficient. * **Data mutability**: If preserving the original array's integrity is crucial, using `concat` might be a better choice. **Special JavaScript features and syntax** In this benchmark, no special JavaScript features or syntax are used. However, it's essential to note that some JavaScript engines might have performance optimizations or quirks when dealing with array methods like `unshift` or `concat`. Always consider the specific implementation details of your target browser or engine. **Alternatives** Other alternatives for adding elements to an array at the beginning include: * Using a data structure like a linked list or a stack, which might provide better performance for large arrays. * Utilizing a library or framework that provides optimized array manipulation functions, such as Lodash's `unshift` method. * Implementing a custom array implementation from scratch, if you need specific performance characteristics. In summary, the benchmark compares two common approaches to adding elements to an array at the beginning: `unshift` and `concat`. By understanding the pros and cons of each approach, developers can make informed decisions about which method to use depending on their specific requirements and constraints.
Related benchmarks
Concat() vs. Unshift()
Array add vs .unshift() 2
Comments
Confirm delete:
Do you really want to delete benchmark?