Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map
(version: 0)
Comparing performance of:
Set vs Object vs Map
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Set
var set = new Set(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]);
Object
var set = {"one": true, "two": true, "three": true, "four": true, "five": true, "six": true, "seven": true, "eight": true, "nine": true, "ten": true};
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]]);
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:
26 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
2763992.5 Ops/sec
Object
68005776.0 Ops/sec
Map
2720212.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. The benchmark compares three data structures in JavaScript: Sets, Objects, and Maps. The test cases are designed to measure their performance and execution speed. Here's an explanation of each test case: 1. **Set**: A Set is a collection of unique values. In this test case, a new Set is created with 10 elements using the `new Set()` constructor. * Pros: Sets provide fast lookups, insertions, and deletions, making them suitable for use cases that require frequent additions or removals of elements. * Cons: Sets are not ordered and do not support key-value pairs, which might be a limitation in certain scenarios. 2. **Object**: An Object is an unordered collection of key-value pairs. In this test case, a new object is created with 10 key-value pairs using the `{}` syntax. * Pros: Objects provide fast lookups by key and can store additional metadata like methods or properties. * Cons: Objects do not enforce uniqueness among their keys, which might lead to collisions if used as Sets. 3. **Map**: A Map is a collection of key-value pairs that preserves the insertion order. In this test case, a new Map is created with 10 key-value pairs using the `new Map()` constructor. * Pros: Maps provide fast lookups by key and can store additional metadata like methods or properties. * Cons: Maps are not as widely supported as Sets and Objects in older browsers. In terms of special JavaScript features, none are explicitly used in this benchmark. However, it's worth noting that some modern browsers support features like `Symbol` keys, which could be used to create more efficient data structures. But for the sake of simplicity, this benchmark uses only the built-in Set, Object, and Map constructors. Now, let's talk about other alternatives: * **Arrays**: While not directly comparable to Sets, Objects, and Maps, arrays can also be used as collections in JavaScript. However, they are less suitable for fast lookups or unique elements. * **Typed Arrays**: For numerical data, typed arrays like `Uint8Array` or `Float64Array` can provide better performance than traditional arrays. * **WeakMaps**: A WeakMap is a Map that only stores weak references to its keys. It's used to avoid memory leaks when the keys are no longer referenced elsewhere in the application. These alternatives might be useful in specific scenarios, but for this benchmark, Sets, Objects, and Maps provide a good starting point for comparing their performance characteristics. In terms of library usage, none are mentioned explicitly in the provided code. However, if you're using external libraries like Lodash or Moment.js, they can introduce additional overhead that might impact performance when running benchmarks like this one. Overall, this benchmark provides a straightforward comparison of Sets, Objects, and Maps in JavaScript, highlighting their strengths and weaknesses for common use cases.
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?