Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare operations on Object / Set
(version: 1)
Comparing performance of:
object vs set
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
object
const set = {a: null, b: null, c: null, d: null, e: null} let i = 1 while (i < 999999) { const key = i.toString() set[key] = null const hasKey = !![key] delete key i++ }
set
const set = new Set(['a', 'b', 'c', 'd', 'e']) let i = 1 while (i < 999999) { const key = i.toString() set.add(key) const hasKey = set.has(key) set.delete(key) i++ }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object
set
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 dive into the explanation of the provided benchmark. **What is being tested?** The benchmark measures the performance of two different approaches for checking if a key exists in an object or set: 1. **Object**: The test creates an object with 999,999 properties (from `a` to `e`) and then iterates over the range from 1 to 999,999 while adding, checking, and deleting keys. 2. **Set**: The test creates a Set with 5 initial elements (`['a', 'b', 'c', 'd', 'e']`) and then iterates over the same range as above while adding, checking if present in the set using `has`, and deleting elements. **Options compared** The two options being compared are: * Using an object to store keys * Using a Set to store unique values **Pros and Cons of each approach:** 1. **Object**: * Pros: + Can store arbitrary data (not limited to unique values). + Can be used for other purposes beyond just checking key existence. * Cons: + Performance can degrade significantly if the object grows too large, as lookup and deletion operations become slower. 2. **Set**: + Pros: + Optimized for fast membership testing (checking if a value exists in the set). + Memory usage is more predictable and controlled, as sets cannot have duplicate values. * Cons: + Limited to storing unique values only. **Library/ syntax considerations:** The test code uses the built-in `Set` constructor to create a Set. No external libraries are required. No special JavaScript features or syntax are used in this benchmark. **Alternatives** Other data structures that can be used for fast membership testing include: * **Map**: A Map is similar to an object but provides faster lookup and deletion operations, especially when the keys are hashable. * **Array**: An array can be used with `includes()` or `indexOf()` methods for fast membership testing. However, using an object or Set as in this benchmark may still be the most suitable choice depending on the specific use case requirements. In general, if you need to store arbitrary data and perform other operations beyond just checking key existence, an object might be a better fit. If you only need to perform fast membership testing with unique values, a Set is likely a better choice.
Related benchmarks:
Lodash isEqual compare with custom deepEqual in compare objects
Lodash isEqual test vs strict equality check
Lodash isEqual test vs Custom Recursive Function
Compare Two Objects
Lodash isEqual compare with custom deepEqual in compare objects 1
Comments
Confirm delete:
Do you really want to delete benchmark?