window.testArray = [1, 'a', undefined, null, false, { a: 123 }, [1, 2, 3]];
window.arraySlice = Array.prototype.slice;
window.arrayConcat = Array.prototype.concat;
const a = window.testArray.slice(0);
const a = arraySlice.call(window.testArray);
const a = window.testArray.concat([]);
const a = window.arrayConcat.call(window.testArray, []);
const a = [window.testArray];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice [direct call] | |
Array.prototype.slice [saved function call] | |
Array.prototype.concat [direct call] | |
Array.prototype.concat [saved function call] | |
Spread operator |
Test name | Executions per second |
---|---|
Array.prototype.slice [direct call] | 4333405.0 Ops/sec |
Array.prototype.slice [saved function call] | 3435927.5 Ops/sec |
Array.prototype.concat [direct call] | 3303569.2 Ops/sec |
Array.prototype.concat [saved function call] | 2131310.2 Ops/sec |
Spread operator | 3454584.5 Ops/sec |
Let's dive into the benchmark and explain what is being tested.
Benchmark Definition
The benchmark is testing three different approaches to create an array:
Array.slice()
: Directly calls the slice()
method on the original array (window.testArray
).Array.concat()
: Directly calls the concat()
method on the original array (window.testArray
) with an empty array as its argument.window.testArray
) using the syntax const a = [...window.testArray];
.Options being compared
The benchmark is comparing the execution time and performance of each approach.
Array.slice()
:Array.slice()
: Using a saved version of the slice()
method.Array.concat()
: Directly calls the concat()
method on the original array (window.testArray
) with an empty array as its argument.Array.concat()
: Using a saved version of the concat()
method.window.testArray
) using the syntax const a = [...window.testArray];
.Libraries and special JS features
There are no explicit libraries mentioned in this benchmark. However, it does use JavaScript features that might not be supported by all browsers:
=>
): Used implicitly in the saved function calls to Array.slice()
and Array.concat()
.\r\n
) used for formatting the script preparation code.Other alternatives
There are other ways to create an array slice or concatenate an array, such as using loops or recursive functions. However, these approaches are generally less efficient and more verbose than using the methods and spread operator tested in this benchmark.
For example:
const result = [];
for (let i = 0; i < window.testArray.length; i++) {
result.push(window.testArray[i]);
}
concat()
method.In summary, this benchmark tests the performance of different approaches to create an array slice and concatenate an array, including direct calls, saved function calls, and the Spread Operator.