Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map (has/in) vs array
(version: 0)
Compare the speed to retrieve a value from three data structures that can be used for boolean referencing; i.e. for mapping enabled settings.
Comparing performance of:
Set vs Object vs Map vs Array
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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);
Tests:
Set
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');
Object
'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
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');
Array
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');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Set
Object
Map
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
15094253.0 Ops/sec
Object
664994688.0 Ops/sec
Map
14042196.0 Ops/sec
Array
1238916.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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** 1. **Set**: A Set is an unordered collection of unique values. In this benchmark, a Set is created with 10 strings. 2. **Object**: An Object is a collections of key-value pairs. In this benchmark, an object is created with 10 properties, each containing the string "true". 3. **Map**: A Map is similar to an Object, but it's more flexible and can store values of different data types. In this benchmark, a Map is created with 10 key-value pairs. 4. **Array**: An Array is a contiguous collection of values. In this benchmark, an array is created with the same 10 strings as the Set. **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: * For Sets: `set.has('value')` * For Objects: `'key' in object` * For Maps: `map.has('key')` * For Arrays: `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. * Template literals (`''`) are used to define the keys for the Object and Map. * The `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: 1. **Set**: * Pros: Fast lookups, no duplicates. * Cons: Slow iteration, limited functionality. 2. **Object**: * Pros: Fast lookups, flexible key-value pairs. * Cons: Slow iteration, limited scalability. 3. **Map**: * Pros: Flexible key-value pairs, fast lookups. * Cons: More complex than Objects, slower iteration in some cases. 4. **Array**: * Pros: Fast iteration, scalable. * Cons: Slow lookups, requires bounds checking. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Hash tables**: A hash table is similar to an Object or Map, but it's optimized for fast lookups and insertion/deletion operations. 2. **Sparse arrays**: A sparse array is an array where most elements are null or undefined, which can be faster for lookup-heavy workloads. 3. **Trie data structures**: A Trie (also known as a prefix tree) is a data structure that's optimized for fast lookups and prefix matching. 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.
Related benchmarks:
Map has vs get
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
set.has vs. array.includes vs. map.has
Comments
Confirm delete:
Do you really want to delete benchmark?