var params = [ "hello", true, 7 ];
var other = params.slice();
var params = [ "hello", true, 7 ]
var other = [ params ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 4251300.0 Ops/sec |
spread operator | 2759299.0 Ops/sec |
Overview
The provided JSON represents a JavaScript microbenchmarking test case created using MeasureThat.net. The benchmark compares the performance of two approaches: the traditional concat()
method and the new ES6 spread operator, both used to create a copy of an array.
What is tested
The benchmark tests how efficient each approach is in creating a shallow copy of an array, specifically when dealing with arrays containing primitive values (like strings and numbers) and a boolean value. The test cases are designed to push the arrays into memory, allowing the browser's garbage collector to kick in, which can affect performance.
Options compared
The benchmark compares two options:
concat()
method: This is a traditional way of creating a copy of an array by concatenating the original array with an empty array.var params = [ "hello", true, 7 ];
var other = params.concat();
...
): This is a new ES6 feature that allows spreading the elements of an array into a new array.var params = [ "hello", true, 7 ];
var other = [...params];
Pros and Cons
concat()
method:push()
method....
):concat()
.Library usage
There is no explicit library used in this benchmark, but some browsers may have built-in optimizations or implementations for concat()
and spread operator that could affect performance. However, MeasureThat.net abstracts away these variations to focus on the core functionality of each approach.
Special JS feature or syntax
The benchmark uses the ES6 spread operator (...
), which is a relatively new feature introduced in ECMAScript 2015 (ES6). This syntax allows for concise creation of arrays and other iterable objects, making it a popular choice among modern developers. However, as mentioned earlier, its support may be limited in older browsers or environments.
Other alternatives
If you were to rewrite this benchmark using alternative approaches, some options could include:
Array.prototype.slice()
with the spread operator: [...params.slice()]
. This approach avoids creating a new array object but still benefits from the performance advantages of spread.Buffer
or TypedArray
. These APIs can provide more control over memory allocation and garbage collection, potentially leading to better performance.cloneDeep()
or deepCopy()
, which provides a robust implementation for creating deep copies of arrays and objects.Keep in mind that the choice of alternative approaches depends on your specific use case, target browser support, and desired level of complexity.