Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map doing stuff
(version: 0)
Comparing performance of:
Set vs Object vs Map
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Set
const set = new Set(); for (let i = 0; i < 10000; ++i) { set.add(`key_${i}`); } for (let j = 0; j < 10000; ++j) { set.add(`key_${j}`); } let result = 0; for (let k = 0; k < 20000; ++k) { result += set.has(`key_${k}`); } console.log(result);
Object
const obj = {}; for (let i = 0; i < 10000; ++i) { obj[`key_${i}`] = true; } for (let j = 0; j < 10000; ++j) { obj[`key_${j}`] = 1; } let result = 0; for (let k = 0; k < 20000; ++k) { result += obj.hasOwnProperty(`key_${k}`); } console.log(result);
Map
const map = new Map(); for (let i = 0; i < 10000; ++i) { map.set(`key_${i}`, true); } for (let j = 0; j < 10000; ++j) { map.set(`key_${j}`, 1); } let result = 0; for (let k = 0; k < 20000; ++k) { result += map.get(`key_${k}`); } console.log(result);
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 what's being tested in the provided benchmark. The benchmark is comparing three data structures: Sets, Objects, and Maps. Each test case uses a different data structure to perform a specific operation: 1. **Sets**: The first test case uses a `Set` object to store keys and check if they exist using the `has()` method. 2. **Objects**: The second test case uses an Object literal (`{}`) to store key-value pairs, where each value is either `true` or a numeric value (`1`). It then checks if a key exists using the `hasOwnProperty()` method. 3. **Maps**: The third test case uses a `Map` object to store keys and values, where each value is associated with a string value (`true` or `'1'`). It then retrieves the value associated with a key using the `get()` method. **Options being compared:** * Set vs Object: Both data structures are used to store key-value pairs, but they have different properties and methods for checking existence (e.g., `has()` in Sets, `hasOwnProperty()` in Objects). * Map vs Object: Maps are a more modern data structure that allows efficient lookups using keys, while Objects require slower lookups using the `in` operator or property names. **Pros and Cons of each approach:** 1. **Set vs Object**: * Pros: + Sets provide faster lookup times for existence checks (average O(1) time complexity). + Sets are more concise and easier to work with when dealing with a large number of unique values. * Cons: + Sets do not preserve the order of elements, whereas Objects do. 2. **Map vs Object**: * Pros: + Maps provide faster lookups using keys (average O(1) time complexity). + Maps are more memory-efficient when dealing with a large number of key-value pairs. * Cons: + Maps require JavaScript 1.8 or later to be supported, whereas Objects work in older versions of JavaScript. **Library and purpose:** The `Map` data structure is a built-in part of modern JavaScript (since ECMAScript 2015), while Objects are also native to JavaScript. The use of these data structures depends on the specific requirements of your application and personal preference. **Special JS features or syntax:** None mentioned in this benchmark, but note that this benchmark uses ES6+ features like arrow functions (`=>`), template literals (`\r\n`), and `const` declarations. **Other alternatives:** If you need to compare performance with other data structures, consider using: 1. **Arrays**: Although less efficient than Sets or Maps for lookup-heavy operations, arrays can be useful when dealing with sequential data or specific use cases like searching. 2. **WeakSets**: For storing and checking the existence of objects without preserving their order or modifying them. This benchmark is focused on comparing the performance of different data structures in JavaScript for a common use case (existence checks).
Related benchmarks:
Map vs Object
Map vs object for deletions
Switch vs Object Literal vs If Else vs Map - testing with simpler data again again
Map.set vs Object assign
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?