const testArray = Array.from({ length: 10000 }, (_, i) => i)
window.testArray = testArray
window.testArray = window.testArray.filter(elem => elem !== 9000);
const index = window.testArray.findIndex(elem => elem === 9000)
window.testArray.splice(index, 1)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 |
Test name | Executions per second |
---|---|
1 | 4661.5 Ops/sec |
2 | 108157.6 Ops/sec |
Let's break down what's being tested in the provided JSON benchmark.
Script Preparation Code
The script preparation code creates an array testArray
with 10,000 elements, where each element is its index from 0 to 9,999. This array is then assigned to the global variable window.testArray
.
Benchmark Definitions There are two individual test cases:
testArray
using the filter()
method.testArray
using the findIndex()
method, and then removes it using the splice()
method.Options Compared The two test cases compare different approaches to remove an element from the array:
filter()
, which creates a new array with all elements that pass the provided condition (in this case, not equal to 9000). This approach has the advantage of being efficient and predictable, but it also creates a new array, which can be memory-intensive for large datasets.findIndex()
followed by splice()
, which finds the index of the element to remove and then removes it from the original array. This approach is more efficient than creating a new array, as it modifies the existing array in place.Pros and Cons
Library Usage
None of the test cases explicitly use any libraries, but they do rely on built-in JavaScript methods like filter()
, findIndex()
, and splice()
.
Special JS Features or Syntax
There are no special JavaScript features or syntax mentioned in this benchmark. However, it's worth noting that the use of window.testArray
is not a standard JavaScript practice and may be specific to MeasureThat.net's framework.
Other Alternatives
map()
instead of filter()
: Instead of creating a new array with filter()
, you could use map()
to create an array with all elements that pass the condition, but then remove the original array.slice()
instead of splice()
: Instead of removing an element from the end of the array using splice()
, you could use slice()
to create a new array with all elements except the one you want to remove.Keep in mind that these alternatives may have different performance characteristics and might not be suitable for all scenarios.