Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map 3
(version: 0)
Comparing performance of:
Set vs Object vs Map
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Set
const set = new Set(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]); for (let i = 0; i < 1_000_000; i++) { const hasSeven = set.has('seven'); }
Object
const set = {"one": true, "two": true, "three": true, "four": true, "five": true, "six": true, "seven": true, "eight": true, "nine": true, "ten": true}; for (let i = 0; i < 1_000_000; i++) { const hasSeven = set['seven'] === true; }
Map
const set = new Map([["one", true], ["two", true], ["three", true], ["four", true], ["five", true], ["six", true], ["seven", true], ["eight", true], ["nine", true], ["ten", true]]); for (let i = 0; i < 1_000_000; i++) { const hasSeven = set.has('seven'); }
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents three test cases for measuring the performance of JavaScript's Set, Object, and Map data structures. **Options being compared:** 1. **Set**: A Set is an unordered collection of unique values. In this benchmark, it's used to store a list of strings and then check if a specific string ("seven") exists in the set. 2. **Object**: An object is an unordered collection of key-value pairs. In this benchmark, it's used to store a list of boolean values with keys as strings, and then check if a specific value is present in the object using a bracket notation (`set['seven'] === true`). 3. **Map**: A Map is an ordered collection of key-value pairs. In this benchmark, it's used to store a list of key-value pairs (strings-boolean) and then check if a specific key exists in the map. **Pros and Cons:** * **Set:** Pros: + Fast lookups since Sets use hash tables. + Efficient for storing unique values. * Cons: + Not suitable for storing ordered data or key-value pairs. * **Object:** Pros: + Can store arbitrary JavaScript objects as values. + Supports bracket notation for accessing values by key. * Cons: + Lookups can be slower than Sets due to the need to iterate over properties. + Inefficient for large datasets since it uses a hash table with string keys. * **Map:** Pros: + Fast lookups and efficient for storing ordered key-value pairs. + Supports iterating over keys using the `forEach` method. * Cons: + Less convenient than Sets or Objects for accessing values by key. **Library and syntax:** The tests use built-in JavaScript data structures (Set, Object, Map) without any external libraries. The syntax is standard JavaScript, with no special features or syntax being used. **Other considerations:** When choosing between these options, consider the following: * Do you need to store unique values? Use a Set. * Do you need to store ordered key-value pairs? Use a Map. * Do you need to access values by arbitrary keys (e.g., strings)? Use an Object. If you're unsure, start with a Set or a Map, as they provide fast lookups and are more efficient for large datasets. If you need to store ordered data or arbitrary objects as values, use an Object. **Alternatives:** Other data structures in JavaScript that could be used for this benchmark include: * **Array**: While arrays can be used with indexes, they're not as efficient as Sets or Maps for lookups. * **WeakMap**: A WeakMap is similar to a Map but doesn't prevent keys from being garbage collected. It's useful when you don't need to store strong references to values. Keep in mind that these alternatives may have different performance characteristics and use cases, so it's essential to evaluate them based on your specific requirements.
Related benchmarks:
Map vs Object
Map vs object for deletions
Array from() vs Map.keys()
Map.set vs Object assign
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?