var a = Array.from({
length: 1000
}, (_, i) => i + 1)
var b = new Set(a)
return a.includes(900)
return b.has(900)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 21300182.0 Ops/sec |
lookup | 23212226.0 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Definition
The benchmark measures the performance of two JavaScript methods: array.includes()
and Set.has()
. The script preparation code creates an array a
with 1000 elements, each incremented by 1, and converts it to a set b
. The purpose is to test which method is faster when searching for a specific element (900) within these large arrays/set.
Options Compared
Two options are compared:
900
) exists in the array.900
) exists in the set b
.Pros and Cons of Each Approach
Library and Purpose
The Set
data structure is used to store unique values. In this benchmark, it's created from the array a
. Sets are useful when you need to quickly check if an element exists without duplicates or order.
Special JS Feature/Syntax
This benchmark uses a modern JavaScript feature: the Array.from()
method, which creates a new array from an iterable (e.g., object, string) and generates a sequence of values. It's used in the script preparation code to create the large array a
.
Other Alternatives
If you need alternative methods for searching elements within arrays or sets, consider:
includes()
).indexOf()
, includes()
variants (e.g., Array.prototype.includes().namedCapture
), or even some custom solutions using SIMD instructions might be faster.Keep in mind that these alternatives may not always outperform the original approach used here, especially considering polyfill support for older browsers.