Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map (has/in)
(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
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');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Object
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
173572448.0 Ops/sec
Object
2062660480.0 Ops/sec
Map
171999552.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark compares the speed of retrieving values from three different data structures: Sets, Objects, and Maps (specifically using the `has` method). The test cases are designed to retrieve a value for each of 10 keys ("one" through "ten") in each data structure. **Options Compared** 1. **Sets**: A Set is an unordered collection of unique values. In JavaScript, Sets are used to store multiple values without duplicates. 2. **Objects**: An Object is an unordered collection of key-value pairs. In JavaScript, Objects are used to store data with a name (key) and a value. 3. **Maps**: A Map is an object that stores mappings of unique keys to values. Maps are similar to objects but provide additional functionality for storing and retrieving values. **Pros and Cons** * **Sets**: + Pros: Fast lookup, efficient memory usage, and no duplicates. + Cons: Not suitable for storing key-value pairs or complex data structures. * **Objects**: + Pros: Can store any type of value, easy to work with, and widely supported. + Cons: Slow lookup compared to Sets and Maps, may have duplicate keys. * **Maps**: + Pros: Fast lookup, efficient memory usage, and provides a way to iterate over entries (key-value pairs). + Cons: May have slower performance than Sets for simple lookups. **Library and Syntax** In the provided benchmark code, no specific libraries are imported or used. However, it's worth noting that JavaScript objects can be iterated over using methods like `Object.keys()`, `Set.prototype.values()`, and `Map.prototype.entries()`. No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** If you're interested in exploring other data structures or performance benchmarks in JavaScript, here are some alternatives: * **Arrays**: Similar to Sets, but with a fixed size and indexing. * **WeakSets**: A Set-like data structure that can be used to store weak references (i.e., objects that may be garbage collected). * **JSON**: A lightweight data interchange format that can be used for storing key-value pairs or simple data structures. For more performance-focused JavaScript benchmarks, you might want to explore: * micro-benchmarking libraries like `micro-benchmark` or `benchmark.js`. * Online platforms like MeasureThat.net (as shown in this example) or websites like jsperf.com.
Related benchmarks:
Create Object vs Map vs Array vs Set
Set vs Object vs Map, Access
Set vs Object vs Map (has/in) vs array
Set vs Object vs Map, Access (mutable, growing)
Comments
Confirm delete:
Do you really want to delete benchmark?