Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS array concatenation
(version: 0)
Comparing performance of:
concat vs push
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = []; var arr2 = []; for(var i=1; i<100000; i++) { arr1.push(i); } for(var i=100001; i<200002; i++) { arr2.push(i); }
Tests:
concat
const concatenated = arr1.concat(arr2);
push
const concatenated = [...arr1, ...arr2];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
concat
push
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):
Let's break down the benchmark and its options. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests two approaches for concatenating arrays: `concat()` and using the spread operator (`...`). **Options Compared** 1. **`concat()`**: The traditional method of concatenating arrays by calling the `concat()` method on one of the arrays. 2. **Using Spread Operator (`...`)**: A modern approach that uses the spread operator to concatenate arrays. **Pros and Cons** * **`concat()`**: + Pros: - Widely supported across browsers - Easy to understand and implement + Cons: - Generally slower than other methods, especially for large arrays - Can lead to unnecessary copies of the array being created * **Using Spread Operator (`...`)**: + Pros: - Fastest way to concatenate arrays in modern JavaScript engines - Creates a new array with no extra copies or allocations + Cons: - Requires support for ES6+ syntax and might not work in older browsers **Library Used** None. This benchmark uses built-in JavaScript methods and features. **Special JS Feature/Syntax** The `...` spread operator is used to concatenate arrays, which is a relatively new feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** Besides the two options compared (`concat()` and using Spread Operator), other approaches for concatenating arrays include: * Using `Array.prototype.push()`: This method appends elements to the end of an array. It can be used to concatenate arrays, but it's not as efficient as using the spread operator. * Using `Array.prototype.concat()`: Similar to `push()`, this method returns a new array with all elements from both arrays. Keep in mind that these alternatives might have different performance characteristics and may not be supported across all browsers or versions of JavaScript.
Related benchmarks:
Copy Array
empty an array in JavaScript?
empty an array in JavaScript?(Yorkie)
empty an array in JavaScript?(Yorkie)1
empty an array in JavaScript and then reassign
Comments
Confirm delete:
Do you really want to delete benchmark?