var list = [];
for (var i = 0; i < 1000 * 1000; i++) {
list.push(i);
}
list.push('slice');
list = list.slice(0, 1000 * 1000 / 2);
list.push('splice');
list.splice(0, 1000 * 1000 / 2);
list.push('shift');
for (var i = 0; i < 1000 * 1000 / 2; i++) {
list.shift();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
splice | |
shift |
Test name | Executions per second |
---|---|
slice | 392.6 Ops/sec |
splice | 3851626.8 Ops/sec |
shift | 21.4 Ops/sec |
I'd be happy to explain the benchmark and its various aspects.
Benchmark Overview
The benchmark is designed to compare the performance of three JavaScript methods: slice()
, splice()
, and shift()
. These methods are used to manipulate arrays in JavaScript. The goal of this benchmark is to determine which method is the fastest when keeping a constant size 2 for a large array.
Script Preparation Code
The script preparation code is provided as:
var list = [];
for (var i = 0; i < 1000 * 1000; i++) {
list.push(i);
}
This code creates an empty array list
and then populates it with 1 million elements using a loop. The purpose of this code is to create a large array that will be used for the benchmark.
Html Preparation Code
There is no Html preparation code provided, which means that the benchmark does not include any HTML-related overhead or distractions.
Individual Test Cases
The individual test cases are defined as:
[
{
"Benchmark Definition": "list.push('slice');\r\nlist = list.slice(0, 1000 * 1000 / 2);",
"Test Name": "slice"
},
{
"Benchmark Definition": "list.push('splice');\r\nlist.splice(0, 1000 * 1000 / 2);",
"Test Name": "splice"
},
{
"Benchmark Definition": "list.push('shift');\r\nfor (var i = 0; i < 1000 * 1000 / 2; i++) {\r\n list.shift();\r\n}",
"Test Name": "shift"
}
]
These test cases define three different scenarios:
slice()
: Pushes an element onto the array and then uses slice()
to create a new array containing only half of the original elements.splice()
: Pushes an element onto the array and then uses splice()
to remove half of the original elements from the beginning of the array.shift()
: Pushes an element onto the array and then repeatedly removes elements from the beginning of the array using shift()
.Library
There is no explicit library mentioned in the benchmark, but it's likely that the benchmark is using the built-in JavaScript methods slice()
and splice()
which are part of the ECMAScript standard.
Special JS Feature or Syntax
There is no special JS feature or syntax used in this benchmark. The code uses only basic JavaScript constructs such as arrays, loops, and method calls.
Other Alternatives
If you were to rewrite this benchmark using alternative methods, here are a few options:
Keep in mind that these alternatives may change the nature of the benchmark and potentially introduce new biases or complexities.