Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set Vs Map Vs Object
(version: 0)
Comparing performance of:
Set vs Map vs Object
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Set
const REPEAT = 1000000 const set = new Set() for (let i = 0; i < REPEAT; i++) { if (!set.has(`${i}`)) { set.add(`${i}`) } }
Map
const REPEAT = 1000000 const set = new Map() for (let i = 0; i < REPEAT; i++) { if (!set.has(`${i}`)) { set.set(`${i}`, true) } }
Object
const REPEAT = 1000000 const set = {} for (let i = 0; i < REPEAT; i++) { if (!set[`${i}`]) { set[`${i}`] = true } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Map
Object
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark tests the performance of three data structures in JavaScript: Sets, Maps, and Objects. The goal is to determine which one performs best in terms of execution speed. **Data Structures Compared** 1. **Set**: A Set is a collection of unique values. In this benchmark, we create a new Set instance and add elements to it using the `add()` method. 2. **Map**: A Map is an object that stores key-value pairs. In this benchmark, we create a new Map instance and set key-value pairs using the `set()` method. 3. **Object**: An Object is a collection of key-value pairs. In this benchmark, we create an empty object and add elements to it using the bracket notation (`[]`). **Options Compared** The options being compared are: * Using a Set: adds elements to the set using the `add()` method * Using a Map: sets key-value pairs using the `set()` method * Using an Object: adds elements to the object using the bracket notation (`[]`) **Pros and Cons of Each Approach** 1. **Set**: Pros: * Faster lookup and insertion times compared to Maps. * Less memory overhead due to unique values. 2. **Map**: Pros: * More flexible than Sets, as it allows for key-value pairs. * Can be used with iterators and other Map-specific methods. 3. **Object**: Pros: * Most similar to the built-in JavaScript object type. * Can be used with existing code that assumes an object is present. Cons: 1. **Set**: * Slower performance compared to Maps for large datasets. * Less flexible than Maps, as it only supports unique values. 2. **Map**: * More memory-intensive due to the key-value pair overhead. * Requires explicit key handling and may lead to errors if not used carefully. 3. **Object**: * Less performant compared to Sets for large datasets. * May lead to slower lookup times due to the object's inherent property-based lookups. **Library or Special JS Feature Used** None of the benchmark definitions explicitly use any libraries or special JavaScript features beyond the standard Set, Map, and Object APIs. **Alternative Alternatives** If you're looking for alternative data structures in JavaScript, consider: 1. **WeakSet**: Similar to a Set but with some key differences in how it handles object references. 2. **WeakMap**: Similar to a Map but with some key differences in how it handles object references and iterator usage. 3. **Proxy Objects**: Allow for custom behavior when accessing or manipulating properties of an object, which can be useful for implementing caching or validation logic. These alternatives may offer improved performance or flexibility for specific use cases, but they require careful consideration and testing to ensure compatibility with your existing codebase.
Related benchmarks:
Map vs Object
Object.setPrototypeOf vs Object literal
Map vs object for deletions
Map.set vs Object assign
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?