var list = [];
for (var i = 0; i < 1000 * 1000; i++) {
list.push(i);
}
list.push('slice');
list = list.slice(1);
list.push('splice');
list.splice(1, list.length-1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
splice |
Test name | Executions per second |
---|---|
slice | 404.5 Ops/sec |
splice | 5174035.0 Ops/sec |
Benchmark Explanation
The provided benchmark is designed to measure the performance difference between three approaches for manipulating arrays in JavaScript: slice
, splice
, and shift
. The benchmark consists of two test cases:
slice
: Creates a copy of the array using the slice()
method.splice
: Removes elements from the end of the array using the splice()
method.The benchmark generates an array with 100,000 elements and then performs one million pushes to this array. The performance difference between these three approaches is then measured in terms of execution time per second.
Comparison Options
There are two main comparison options:
slice
: Creates a new array by copying the original array using slice()
.splice
: Modifies the original array in place by removing elements from the end.slice
since it modifies the original array without creating a copy.Other Considerations
In addition to these two options, there is another approach that uses the shift()
method:
shift
: Removes elements from the beginning of the array without creating a new copy.slice
since it modifies the original array without creating a copy.Library and Special JS Features
There is no library used in this benchmark. However, the benchmark does utilize some special JavaScript features:
slice()
, splice()
, and shift()
methods to manipulate arrays.for
loop to push elements onto the array.Benchmark Result
The latest benchmark result shows that the Chrome 119 browser with a Desktop platform and Mac OS X 10.15.7 operating system achieved:
splice
: Execution time of approximately 9,019,286 executions per second.slice
: Execution time of approximately 383 executions per second.These results indicate that the splice()
method is significantly faster than the slice()
method in this benchmark.