var a = [];
for (var i = 0; i < 1E6; i++)
a[i] = i;
var b = new Set(a)
return a.includes(9E5)
return b.has(9E5)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 2019.6 Ops/sec |
lookup | 22220590.0 Ops/sec |
I'll break down the benchmark and its components, explaining what's being tested and the options compared.
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark for testing two approaches to check if an element exists in a set or array: set.has()
vs array.includes
.
Script Preparation Code
The script preparation code creates two variables:
a
: An empty array with 1 million elements, each assigned a unique value using the loop.b
: A new Set object initialized with the values from a
.Options Compared
Two test cases are compared:
a
using the array.includes()
method.b
using the set.has()
method.Approaches Compared
Both approaches have their pros and cons:
includes
):lookup
):array.includes()
.Library Used
The library used in this benchmark is JavaScript itself, with no external libraries required. However, the Set data structure is a built-in JavaScript feature that provides fast and efficient way to store unique values.
Special JS Feature/ Syntax
This benchmark doesn't rely on any special JavaScript features or syntax. It only uses standard JavaScript methods and data structures.
Other Alternatives
For sets, you could use other libraries like Lodash's new Set()
function or the Set
class from the TypeScript library. For arrays, you might consider using a data structure like a hash table or a trie for faster lookups.
Keep in mind that these alternatives are not necessarily necessary for this specific benchmark, as JavaScript's built-in Set and array methods provide sufficient performance for most use cases.