Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Value exists in Set vs Object vs Map
(version: 0)
Comparing performance of:
Set vs Object vs Map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]; var randomValue = values[Math.round(Math.random() * 9)]
Tests:
Set
var set = new Set(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]); var inSet = set.has(randomValue);
Object
var set = {"one": true, "two": true, "three": true, "four": true, "five": true, "six": true, "seven": true, "eight": true, "nine": true, "ten": true}; var inSet = set[randomValue];
Map
var set = new Map([["one", true], ["two", true], ["three", true], ["four", true], ["five", true], ["six", true], ["seven", true], ["eight", true], ["nine", true], ["ten", true]]); var inSet = set.has(randomValue)
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):
Let's break down the provided JSON benchmark and explain what's being tested. **Benchmark Purpose:** The goal of this benchmark is to compare the performance of three data structures in JavaScript: Sets, Objects, and Maps. Specifically, it measures how fast it can check if a random value exists within each data structure. **Data Structures Comparison:** 1. **Set:** A Set is an unordered collection of unique values. In this benchmark, we create a Set with an array of strings and then check if a randomly selected string exists in the Set using the `has` method. 2. **Object:** An Object is an unordered collection of key-value pairs. In this benchmark, we create an Object with boolean values (true or false) as keys and then check if the randomly selected value exists in the Object using bracket notation (`set[randomValue]`). 3. **Map:** A Map is an ordered collection of key-value pairs. In this benchmark, we create a Map with arrays of strings as keys and boolean values (true or false) as values. We then check if the randomly selected string exists in the Map using the `has` method. **Pros and Cons:** * **Set:** Fast lookups because Sets only store unique values, eliminating duplicate checks. + Pros: Efficient lookup time due to Set's hash-based storage. + Cons: May not be suitable for applications that require storing key-value pairs with duplicate keys. * **Object:** Slow lookups due to the need to iterate through all properties. + Pros: Can store complex key-value pairs, including objects and arrays as keys. + Cons: Poor performance for large datasets or when searching for a specific value. * **Map:** Fast lookups because Maps use a hash-based data structure with average O(1) time complexity. + Pros: Efficient lookup time due to Map's hash-based storage. + Cons: May not be suitable for applications that require storing key-value pairs with duplicate keys. **Library and Purpose:** None of the benchmarks explicitly mention using any external libraries. However, it's worth noting that all three data structures are native JavaScript constructs, so no additional libraries are required to use them. **Special JS Features or Syntax:** There is no special JS feature or syntax used in this benchmark. The examples focus on the basic implementation of Sets, Objects, and Maps using their respective constructors (`Set()`, `{}`, and `Map()`). **Alternative Approaches:** * **Using Array.includes()**: Instead of checking if a value exists in a Set, Object, or Map, you could use the `Array.includes()` method to check if an element is present in an array. This approach would eliminate the need for creating data structures like Sets and Maps. * **Using a Queue or Stack**: Depending on your specific requirements, you might consider using a queue or stack data structure instead of Sets, Objects, or Maps. Overall, this benchmark provides a useful comparison of performance between three fundamental data structures in JavaScript, highlighting their trade-offs and use cases.
Related benchmarks:
Number to String
toFixed -> Number vs Math.round
string-interpolation-vs-toString
string-interpolation-vs-to-string
toFixed vs Math.round() with numbers222
Comments
Confirm delete:
Do you really want to delete benchmark?