var baseArray = []
for (var i = 0; i < 50; i++) {
baseArray.push((Math.floor(Math.random() * Math.floor(1000000))).toString(16))
}
var other = baseArray.slice();
var other = [ baseArray ]
var other = baseArray.slice(0);
var other = Array.from(baseArray);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice() | |
spread operator | |
Array.prototype.slice(0) | |
Array.from() |
Test name | Executions per second |
---|---|
Array.prototype.slice() | 5681191.5 Ops/sec |
spread operator | 1320724.5 Ops/sec |
Array.prototype.slice(0) | 5724951.5 Ops/sec |
Array.from() | 1247190.9 Ops/sec |
The benchmark defined in the provided JSON compares several methods of creating a shallow copy of an array in JavaScript. The objective is to evaluate the performance of different approaches, specifically focusing on the traditional Array.prototype.slice()
method and several modern alternatives introduced in ES6 (ECMAScript 2015), including the spread operator and Array.from()
.
Array.prototype.slice()
var other = baseArray.slice();
Spread Operator
var other = [ ...baseArray ];
slice()
, though generally efficient.Array.prototype.slice(0)
var other = baseArray.slice(0);
slice()
, but explicitly specifies the start index (0), which makes it clear that a full copy of the array is intended.slice()
; this approach may be seen as redundant.Array.from()
var other = Array.from(baseArray);
slice()
or the spread operator.Based on the latest benchmark results, we can see the following ExecutionsPerSecond
values for each testing method:
From these results, it is evident that:
slice()
.Readability vs. Performance: The choice between these methods can also depend on the context of usage. While some developers might prioritize performance, others might favor code readability and maintainability, particularly in larger codebases.
Browser Compatibility: While methods like Array.from()
and the spread operator are part of ES6, older browsers might require polyfills or—if they are not supported natively—alternative approaches, making compatibility an essential consideration.
Memory Implications: All tested methods perform shallow copies, meaning they only duplicate the references to the original objects in the case of nested structures. Therefore, if the original array contains objects, changes to the objects in one array will reflect in the other.
In addition to the methods tested, developers can consider:
Looping Constructs: Manually iterating through an array using a loop to copy elements can be an option, providing fine-grained control over the copying process but at the cost of brevity and clarity.
Functional Approaches: Using methods like map()
can also achieve similar results, allowing transformations during the copy process, although this is not strictly for copying purposes.
In summary, while Array.prototype.slice()
remains an excellent choice for performance and clarity in copying arrays, modern techniques like the spread operator provide convenient alternatives that might favor readability in contemporary JavaScript code.