Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map, Access (mutable, growing)
(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
window.last ??= performance.now(); set.delete(window.last); set.has(performance.now()); set.add(performance.now()); set.has('one'); set.has('two'); set.has('three'); set.has('four'); set.has('five'); set.delete(performance.now()); set.has(performance.now()); set.add(performance.now()); set.has('six'); set.has('seven'); set.has('eight'); set.has('nine'); set.has('ten'); set.delete(performance.now()); window.last = performance.now(); set.has(window.last); set.add(window.last);
Object
window.last ??= performance.now(); delete object[window.last]; object[performance.now()]; object[performance.now()] = true; object['one']; object['two']; object['three']; object['four']; object['five']; object[performance.now()]; object[performance.now()] = true; object['six']; object['seven']; object['eight']; object['nine']; object['ten']; window.last = performance.now(); object[window.last]; object[window.last] = true
Map
window.last ??= performance.now(); map.delete(window.last); map.get(performance.now()); map.set(performance.now(), true); map.get('one'); map.get('two'); map.get('three'); map.get('four'); map.get('five'); map.get(performance.now()); map.set(performance.now(), true); map.get('six'); map.get('seven'); map.get('eight'); map.get('nine'); map.get('ten'); window.last = performance.now(); map.get(window.last); map.set(window.last, true);
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
401823.6 Ops/sec
Object
776693.9 Ops/sec
Map
789778.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark compares the speed of accessing values from three data structures: Sets, Objects, and Maps. The goal is to determine which data structure is the fastest for boolean referencing, i.e., checking if a value exists or adding/removing a value. **Options Compared** The options being compared are: 1. **Sets**: JavaScript's built-in `Set` object, which stores unique values in an unordered collection. 2. **Objects**: A plain JavaScript Object, where each property is a key-value pair. 3. **Maps**: JavaScript's built-in `Map` object, which stores key-value pairs with optional values. **Pros and Cons of Each Approach** * **Sets**: + Pros: Fast lookup and insertion times due to its hash table implementation. + Cons: May require more memory if the set grows large, as it needs to store additional metadata (e.g., size, capacity). * **Objects**: + Pros: Lightweight, easy to create, and fast for simple lookups using property access (e.g., `obj['key']`). + Cons: Slow for large datasets or complex queries due to the overhead of iterating over properties. * **Maps**: + Pros: Fast lookup and insertion times, similar to Sets, with additional features like optional values and key-value pairs. + Cons: May require more memory than Sets if the map grows large, as it needs to store additional metadata (e.g., size, capacity). **Library Usage** None of the benchmark tests use external libraries. They rely on built-in JavaScript data structures. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Considerations** When choosing a data structure for boolean referencing, consider factors like: * Speed: If speed is critical, Sets and Maps might be better choices. * Memory usage: If memory efficiency is important, Sets might be a better option due to their more compact representation. * Complexity: For simple lookups, Objects might be sufficient. For more complex queries or large datasets, Sets or Maps might be more suitable. **Alternatives** If you're looking for alternative data structures in JavaScript, consider: 1. **WeakSets**: Similar to Sets but with additional features like automatic garbage collection and support for weak references. 2. **WeakMaps**: Similar to Maps but with additional features like automatic garbage collection and support for weak references. 3. **JSON objects**: While not optimized for performance, JSON objects can be used as a lightweight alternative to Sets or Objects. Keep in mind that the choice of data structure ultimately depends on your specific use case and requirements.
Related benchmarks:
Set vs Object vs Map, Access
Set vs Object vs Map (has/in)
Set vs Object vs Map (has/in) vs array
Set vs Object vs Map vs Array, Access
Comments
Confirm delete:
Do you really want to delete benchmark?