var a = Array.from({length: 100000}, () => Math.floor(Math.random() * 40));
var b = new Set(a);
return a.includes(98765)
return b.has(98765)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Includes | |
lookup |
Test name | Executions per second |
---|---|
Includes | 13940.1 Ops/sec |
lookup | 20663170.0 Ops/sec |
I'll break down the benchmark and explain what's being tested.
Benchmark Overview
The MeasureThat.net benchmark is designed to compare the performance of two approaches: using array.includes()
and using a Set
data structure to look up an element.
Test Cases
There are two test cases:
a
with 100,000 random elements, and then checks if a specific value (98765) is present in the array using array.includes()
.b
from the same array a
, and then checks if the same value (98765) is present in the set using Set#has()
.Options Compared
The two options being compared are:
a
) to store elements, and checking for presence using array.includes()
.Set
data structure to store elements, and checking for presence using Set#has()
.Pros and Cons of Each Approach
Array-based approach:
Pros:
Cons:
Set-based approach:
Pros:
Cons:
Set
data structures.Library Used
The library used in this benchmark is not explicitly mentioned. However, it's likely that the JavaScript engine being tested supports modern JavaScript features and standard library functions like Array.from()
and Set#has()
, which are part of the ECMAScript standard.
Special JS Feature or Syntax
There doesn't appear to be any special JavaScript feature or syntax used in this benchmark. The tests are straightforward and rely on standard JavaScript functionality.
Other Alternatives
If you're looking for alternative approaches to performance-critical lookup operations, consider:
Map
data structure instead of an array or set, which provides faster lookups with an average time complexity of O(1).Keep in mind that the best approach will depend on the specific requirements and constraints of your use case.