var a = Array({
length: 1_000_000
}, (v, index) => index)
var b = new Set(a)
return a.includes(9)
return b.has(9)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 16281252.0 Ops/sec |
lookup | 13992622.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The provided JSON represents a benchmark that tests the performance of two different approaches: using array.includes()
and using Set.has()
. The test case is specifically designed to measure the performance of these two methods when searching for an element in a large array (1 million elements).
Options Compared
Two options are compared:
array.includes()
: This method searches for an element in an array by iterating through each element until it finds a match or reaches the end of the array.Set.has()
: This method uses a Set data structure to store unique values. When you call has()
on a Set, it checks if the specified value is present in the Set.Pros and Cons
Here are some pros and cons of each approach:
Library and Purpose
In this benchmark, the Set
library is used. A Set
is a collection of unique values that allows for fast lookups, insertions, and deletions. In this test case, the Set.has()
method is used to quickly check if an element (9) is present in the Set created from the array.
Special JS Feature or Syntax
There are no special JS features or syntax used in this benchmark other than the use of Set
which is a standard JavaScript library.
Other Alternatives
If you want to explore alternative approaches, here are some options:
includes()
but returns an index instead of a boolean value.Map
data structure, which is another type of data structure that allows for fast lookups.Keep in mind that these alternatives may have slightly different performance characteristics compared to using Set.has()
or array.includes()
.