Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unshift vs concat
(version: 0)
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:
3 years ago
by:
Guest
Jump 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 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
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Unshift
26081.7 Ops/sec
Concat
803.1 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:
Array .concat() vs .unshift()
Concat() vs. Unshift()
spread vs concat vs unshift(clone)
Array add vs .unshift() 2
Comments
Confirm delete:
Do you really want to delete benchmark?