Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
concat vs spread vs push
(version: 0)
Comparing performance of:
Array.prototype.concat vs spread operator vs push
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params);
spread operator
var params = [ "hello", true, 7 ] var other = [ 1, 2, ...params ]
push
var params = [ "hello", true, 7 ]; Array.prototype.push.apply([ 1, 2 ], params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
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):
Measuring performance differences between various approaches to concatenating or manipulating arrays in JavaScript. Let's break down the provided benchmark definition and test cases: 1. **Benchmark Definition**: The benchmark is defined as an object with three properties: * `Name`: A descriptive name for the benchmark ("concat vs spread vs push"). * `Description` (null): An optional description of the benchmark, which is not provided in this case. * `Script Preparation Code` (null) and `Html Preparation Code` (null): The code used to prepare the script and HTML for execution, respectively. In this case, they are empty. 2. **Individual Test Cases**: There are three test cases: * **Array.prototype.concat**: + Benchmark Definition: "var params = [ \"hello\", true, 7 ];\r\nvar other = [ 1, 2 ].concat(params);" + Test Name: "Array.prototype.concat" (indicating that this is a test of the `concat()` method on the Array prototype) * **Spread Operator**: + Benchmark Definition: "var params = [ \"hello\", true, 7 ]\r\nvar other = [ 1, 2, ...params ];" + Test Name: "spread operator" (indicating that this is a test of the spread operator syntax) * **push**: + Benchmark Definition: "var params = [ \"hello\", true, 7 ];\r\nArray.prototype.push.apply([ 1, 2 ], params);" + Test Name: "push" (indicating that this is a test of the `push()` method on the Array prototype) Now, let's analyze each approach: * **Array.prototype.concat()**: This method concatenates two or more arrays. It creates a new array and returns it. Pros: + Widely supported in modern browsers Cons: + Creates a new array, which can be inefficient for large datasets + May not be the most efficient option when working with very large arrays * **Spread Operator** (`...`): This syntax allows you to spread an array into another array. Pros: + Creates a shallow copy of the original array, preserving references + Can be more efficient than `concat()` for large datasets Cons: + Requires modern browsers that support the spread operator (introduced in ECMAScript 2018) + May not work as expected if the target array has sparse properties * **Array.prototype.push()**: This method adds one or more elements to an array. Pros: + Can be used with arrays of any size Cons: + Creates a new array, which can be inefficient for large datasets + May not be the most efficient option when working with very large arrays The current benchmark results show that: * The spread operator (`...`) is currently the fastest approach. * `Array.prototype.concat()` is slightly slower than the spread operator but faster than `push()`. * `push()` is the slowest of the three approaches. Other alternatives to consider: * Using `Object.assign()` or a library like Lodash's `concat` function * Using a custom implementation that avoids creating new arrays or objects Keep in mind that the performance differences between these approaches can be significant, especially for large datasets. The choice of approach ultimately depends on your specific use case and requirements.
Related benchmarks:
spread operator vs push Brian
spread operator vs push Brian2
concat vs spread vs push vs push fn
Array concat vs spread operator vs push larger list
zk test spread vs push
Comments
Confirm delete:
Do you really want to delete benchmark?