var set = new Set(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]);
var object = {"one": true, "two": true, "three": true, "four": true, "five": true, "six": true, "seven": true, "eight": true, "nine": true, "ten": true};
var map = new Map([["one", true], ["two", true], ["three", true], ["four", true], ["five", true], ["six", true], ["seven", true], ["eight", true], ["nine", true], ["ten", true]]);
var values = Object.keys(object);
set.has('one');
set.has('two');
set.has('three');
set.has('four');
set.has('five');
set.has('six');
set.has('seven');
set.has('eight');
set.has('nine');
set.has('ten');
'one' in object;
'two' in object;
'three' in object;
'four' in object;
'five' in object;
'six' in object;
'seven' in object;
'eight' in object;
'nine' in object;
'ten' in object;
map.has('one');
map.has('two');
map.has('three');
map.has('four');
map.has('five');
map.has('six');
map.has('seven');
map.has('eight');
map.has('nine');
map.has('ten');
values.includes('one');
values.includes('two');
values.includes('three');
values.includes('four');
values.includes('five');
values.includes('six');
values.includes('seven');
values.includes('eight');
values.includes('nine');
values.includes('ten');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Object | |
Map | |
Array |
Test name | Executions per second |
---|---|
Set | 164715184.0 Ops/sec |
Object | 180006240.0 Ops/sec |
Map | 170064032.0 Ops/sec |
Array | 14400590.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the speed of retrieving values from three data structures: Sets, Objects, Maps, and Arrays. The goal is to determine which approach is fastest for boolean referencing (i.e., checking if a value exists in a collection).
Data Structures Compared
Benchmark Test Cases
Each test case consists of a single line of code that checks if a specific value exists in one of the data structures:
set.has('value')
'key' in object
map.has('key')
array.includes('value')
These test cases are repeated 10 times for each data structure, ensuring that the benchmark captures any potential variations in performance.
Library Used
None of the test cases use external libraries. The data structures (Set, Object, Map, and Array) are native to JavaScript.
Special JS Features/Syntax
The benchmark uses a few special features and syntax:
var
is used to declare variables.new
is used to create new instances of Set, Object, and Map.''
) are used to define the keys for the Object and Map.includes()
method is used to check if an element exists in an array.Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
Other Alternatives
If you're interested in exploring alternative approaches, here are a few options:
Keep in mind that each alternative has its own trade-offs and use cases, so it's essential to carefully evaluate your specific requirements before choosing an approach.