Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map a4vw343v43
(version: 0)
Comparing performance of:
Set vs Object vs Map
Created:
3 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"]); set.add("eleven"); if(set.has("seven")){};
Object
var set = {"one": null, "two": null, "three": null, "four": null, "five": null, "six": null, "seven": null, "eight": null, "nine": null, "ten": null}; set["eleven"] = null; if(set["seven"]!== undefined){};
Map
var set = new Map([["one", null], ["two", null], ["three", null], ["four", null], ["five", null], ["six", null], ["seven", null], ["eight", null], ["nine", null], ["ten", null]]); set.set("eleven", null); if (set.get("seven") !== undefined){};
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:
6 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
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
7407153.5 Ops/sec
Object
183783616.0 Ops/sec
Map
4967709.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The benchmark being measured is the performance difference between using a `Set`, an `Object`, and a `Map` data structure in JavaScript to store and retrieve elements. **Options Compared:** 1. **Set**: A collection of unique values, ordered or unordered, with fast lookups. 2. **Object**: An unordered collection of key-value pairs, where keys are strings and values can be any type. 3. **Map**: An unordered collection of key-value pairs, similar to an Object, but with additional methods for manipulating the key-value pairs. **Pros and Cons:** * **Set**: + Pros: Fast lookups (O(1)), efficient insertion and removal of elements. + Cons: Limited functionality compared to Objects and Maps, can only store unique values. * **Object**: + Pros: Flexible data structure, can store any type of value, allows for nested objects. + Cons: Slower lookups compared to Sets (O(1) on average), more overhead due to the need to manage properties. * **Map**: + Pros: Fast lookups (O(1)), efficient insertion and removal of elements, additional methods for manipulating key-value pairs. + Cons: Less familiar API than Objects, requires more memory due to the need to store keys. The choice of data structure depends on the specific use case. If you only need to store unique values and perform fast lookups, a `Set` might be the best choice. For more flexible data structures with nested objects or complex key-value pairs, an `Object` or `Map` might be more suitable. **Library Used:** None explicitly mentioned in the benchmark definition. However, it's likely that JavaScript's built-in `Set`, `Object`, and `Map` constructors are used under the hood. **Special JS Features/Syntax:** None explicitly mentioned in the benchmark definition. **Other Alternatives:** 1. **Array**: While not as flexible as Sets or Objects, Arrays can be used to store collections of values with fast lookups (O(1)). 2. **WeakSet**, `WeakMap`: These are similar to Sets and Maps but allow for weak references, which can be useful in certain scenarios. 3. **Sparse arrays**: Some modern JavaScript engines (e.g., V8) support sparse arrays, which are arrays that don't store the index values of missing elements. In summary, this benchmark measures the performance difference between using `Set`, `Object`, and `Map` data structures in JavaScript for storing and retrieving elements. The choice of data structure depends on the specific use case and the desired trade-offs between performance, flexibility, and memory usage.
Related benchmarks:
Map vs object for deletions
Array from() vs Map.keys() vs Map.values() vs spread
Map.set vs Object assign
Array Spread vs Fill vs New Array
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?