var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = new Set(a)
a.includes(10)
b.has(10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array | |
set |
Test name | Executions per second |
---|---|
array | 82069936.0 Ops/sec |
set | 143665728.0 Ops/sec |
This benchmark compares the performance of checking for the existence of an element in an array versus a Set using includes
and has
methods respectively.
Options Compared:
a.includes(10)
): Uses the includes
method to check if the value 10
exists within the array a
.b.has(10)
): Uses the has
method to check if the value 10
exists within the Set b
.Pros and Cons:
includes
performs a linear search through the array, which can be slow for large arrays.Other Considerations:
Alternatives:
Let me know if you have any other questions!