Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs concat vs unshift 2 to join arrays
(version: 1)
Comparing the performance of spread, vs concat vs unshift to join two arrays
Comparing performance of:
spread vs concat vs unshift (drugs)
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = [1,2,3]; var b = [4,5,6];
Tests:
spread
b=[...a,...b];
concat
b=a.concat(b);
unshift (drugs)
var c = []; var aLen = a.length; var bLen = b.length; var i = 0; for (; i < aLen; i++) { c.push(a[i]); } i = 0 for (; i < bLen; i++) { c.push(b[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread
concat
unshift (drugs)
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):
Measuring performance of different ways to join two arrays in JavaScript. The test compares three approaches: 1. **Spread Operator (`b = [...a, ...b];`)**: This approach uses the spread operator to create a new array by spreading the elements of `a` and `b`. It's a modern and concise way to concatenate arrays. 2. **`concat()` method**: This is a built-in method in JavaScript that concatenates two or more arrays into one. 3. **`unshift()` method with loops**: This approach uses the `unshift()` method to add elements to the beginning of an array, and then uses loops to push elements from `a` and `b` into the resulting array. **Pros and Cons:** * Spread Operator: + Pros: concise, modern, efficient. + Cons: not supported in older browsers (before IE 11), may be slower than other methods due to its syntax complexity. * `concat()` method: + Pros: widely supported across browsers and versions, easy to read and understand. + Cons: less efficient than spread operator or `unshift()` with loops, as it creates new arrays and assigns them to a single variable. * `unshift()` method with loops: + Pros: can be more efficient than other methods for large arrays, as it modifies the existing array instead of creating new ones. + Cons: less concise and more verbose than spread operator or `concat()` method. **Library and Special JS Features:** None mentioned in the benchmark code. However, note that the spread operator was introduced in ECMAScript 2015 (ES6) and is supported by most modern browsers. **Other Alternatives:** * Using `Array.prototype.push()` and `push.apply()` to concatenate arrays. * Using `Array.prototype.reduce()` to concatenate arrays. * Using a library like Lodash's `concat` function or other utility functions for array concatenation. In general, the spread operator is a good choice when working with modern JavaScript environments. However, in older browsers or situations where conciseness is not prioritized, other methods may be more suitable.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Concat vs Spread (Two Arrays)
unshift vs spread vs concat
.concat vs. spread
spread vs concat vs unshift to join arrays
Comments
Confirm delete:
Do you really want to delete benchmark?