const ITERATIONS = 500000;
var index = ITERATIONS/2;
var n = Math.random();
var list = [];
for (let i = 0; i < length; i += 1) {
list.push(Math.random());
}
const clone = [list];
const clone = list.slice();
const clone = [list, n];
const clone = list.slice();
clone.push(n);
const clone = [
list.slice(0, index),
list.slice(index + 1),
];
const clone = list.slice();
clone.splice(index, 1);
const clone = [
list.slice(0, index),
n,
list.slice(index + 1),
];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array clone with spread operator | |
Array clone with slice | |
Array addition with spread operator | |
Array addition with slice and push | |
Array removal with spread operator | |
Array removal with slice and splice | |
Array update with spread operator |
Test name | Executions per second |
---|---|
Array clone with spread operator | 6392825.0 Ops/sec |
Array clone with slice | 6868095.5 Ops/sec |
Array addition with spread operator | 2304945.8 Ops/sec |
Array addition with slice and push | 2516468.2 Ops/sec |
Array removal with spread operator | 1555341.4 Ops/sec |
Array removal with slice and splice | 3126453.8 Ops/sec |
Array update with spread operator | 1049134.9 Ops/sec |
Let's dive into the Benchmark Definition and test cases.
Benchmark Purpose: The benchmark tests the performance of different approaches for various array operations in JavaScript:
The benchmark compares two methods for each operation: using the spread operator (...
) and using slice
/splice
.
Options Compared:
...
)slice
...
)slice
followed by push
...
) with a partial arrayslice
followed by splice
Pros and Cons:
Library Used: None mentioned in the Benchmark Definition. However, it's likely that the benchmark is running on a JavaScript engine or browser that uses built-in implementations of these methods.
Special JS Features/Syntax: No special features or syntax are mentioned in the Benchmark Definition. The focus is solely on comparing different array operation approaches.
Alternatives: For further comparison, additional test cases could be added to consider other aspects of array operations, such as:
forEach
, map
)filter
, reduce
)Keep in mind that the benchmark is specifically designed to compare performance and readability of different approaches for specific array operations.