var list = [];
for (var i = 0; i < 1000 * 1000; i++) {
list.push(i);
}
list = [
list.slice(0, 50000),
list.slice(50000 + 1)
];
newList = [list];
newList.splice(50000, 1);
newList = list.filter(i => i !== 50000);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
splice | |
filter |
Test name | Executions per second |
---|---|
slice | 96.0 Ops/sec |
splice | 580.2 Ops/sec |
filter | 59.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's tested in this specific benchmark.
Benchmark Overview
The benchmark tests three different approaches to delete an item from a large list: slice
, splice
, and filter
. The goal is to determine which approach performs better in terms of execution speed.
Approaches Compared
slice()
method to create a new array that includes a subset of elements from the original array.splice()
method to remove an element at a specified index from the original array.filter()
method to create a new array with only the elements that pass a test (in this case, not being equal to 50000).Pros and Cons of Each Approach
Library and Purpose
None of these approaches use a library specifically for this benchmark. However, slice()
and splice()
are built-in methods that are part of the ECMAScript standard.
Special JS Feature or Syntax
There is no special JavaScript feature or syntax used in this benchmark. The code only uses basic array operations and methods.
Other Alternatives
If you want to test other approaches for deleting an item from a list, some alternatives could be:
map()
with a callback function that returns undefined
for the desired element.reduce()
with a callback function that removes the desired element from the accumulator.forEach()
and modifying the array in place using splice()
.These alternatives would likely have different performance characteristics than the approaches tested in this benchmark.