var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2, params ]
var params = [ "hello", true, 7 ];
var other = params.concat([ 1, 2 ]);
var params = [ "hello", true, 7 ]
var other = [ params, 1, 2 ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat Append | |
spread operator Append | |
Array.prototype.concat Preppend | |
spread operator Preppend |
Test name | Executions per second |
---|---|
Array.prototype.concat Append | 10973729.0 Ops/sec |
spread operator Append | 30906408.0 Ops/sec |
Array.prototype.concat Preppend | 9895801.0 Ops/sec |
spread operator Preppend | 20817246.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
The provided JSON represents a benchmark test case on MeasureThat.net, which compares the performance of different approaches when concatenating arrays in JavaScript.
What is tested:
The benchmark tests four scenarios:
concat()
method to append elements to an existing array....
) to append elements to an existing array.concat()
method to prepend elements to an existing array....
) to prepend elements to an existing array.Options compared:
The benchmark compares two approaches for each scenario:
concat()
with appending/prependingPros and Cons of different approaches:
concat()
approach:concat()
for large arrays.Library usage:
None of the benchmark test cases explicitly use any libraries. The examples provided only demonstrate basic JavaScript syntax.
Special JS feature/syntax:
The benchmark uses the new ES6 spread operator (...
), which was introduced in ECMAScript 2015 (ES6). This allows for more concise and expressive code when working with arrays.
Other alternatives:
If you're looking for alternative approaches to concatenating arrays, consider using:
Array.prototype.push()
: This method appends elements to the end of an array without creating a new array object.reduce()
or slice()
: These methods can be used to create a new array by iterating over and combining elements from an existing array.Keep in mind that these alternatives might have different performance characteristics depending on the specific use case and browser environment.