var a = [
9,
2,
5,
6,
];
var b = new Set(a)
return a.includes(5)
return b.has(5)
return a.indexOf(5) >= 0
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup | |
indexof |
Test name | Executions per second |
---|---|
includes | 17615174.0 Ops/sec |
lookup | 17973104.0 Ops/sec |
indexof | 15966845.0 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Definition
The benchmark is designed to compare the performance of three different approaches for checking if an element exists in an array or a Set:
array.includes(element)
Set.has(element)
array.indexOf(element) >= 0
These approaches are used to find out whether a specific value (5
) exists in the array or Set created from the array.
Options Compared
The benchmark compares three different options for checking if an element exists in an array:
array.includes(element)
: This method returns a boolean value indicating whether the element is present in the array.Set.has(element)
: This method returns a boolean value indicating whether the element is present in the Set.array.indexOf(element) >= 0
: This approach uses the indexOf
method to find the index of the element, and then checks if it's greater than or equal to 0 (i.e., the element exists).Pros and Cons
Here are some pros and cons of each approach:
array.includes(element)
:Set.has(element)
:array.indexOf(element) >= 0
:Set.has(element)
due to the linear search algorithm used in indexOf
, especially for large arrays.Library and Its Purpose
In this benchmark, the library used is not explicitly mentioned. However, it's likely that the Set
data structure from the ECMAScript standard is being used, which provides a way to store unique values in a data structure with fast lookup times.
Special JS Features or Syntax
There are no special JavaScript features or syntaxes used in this benchmark. The code only uses standard JavaScript features and syntax for arrays and Sets.
Other Alternatives
For checking if an element exists in an array, other alternatives to array.includes(element)
might include:
fast-est-improved-array-find
(FEIF).For checking if an element exists in a Set, other alternatives to Set.has(element)
might include:
Keep in mind that these alternatives would likely require additional overhead and may not be suitable for all use cases.