Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs object
(version: 0)
Comparing performance of:
Set vs Object
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(); var obj = new Object(); function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; }
Tests:
Set
set.add(makeid()); set.has(makeid())
Object
obj[makeid()]=i; if (obj.hasOwnProperty(makeid())) ;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
488370.4 Ops/sec
Object
482588.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. The provided JSON represents two test cases for comparing the performance of JavaScript sets and objects. **Options Compared:** 1. **Sets:** The first test case measures the execution time of adding an element to a Set using `set.add(makeid())` and then checking if that same element exists in the set using `set.has(makeid())`. 2. **Objects:** The second test case measures the execution time of adding an element to an object using `obj[makeid()] = i` and then checking if that same element has a property on the object using `if (obj.hasOwnProperty(makeid()))`. **Pros and Cons:** * **Sets:** Sets are data structures that store unique values, which makes them more efficient for lookups. In this test case, adding an element to a Set is faster because the language can use its internal data structure to keep track of unique elements without having to check every single element individually. * **Objects:** Objects are data structures that store key-value pairs, where each key is unique and maps to a specific value. In this test case, checking if an object has a property using `hasOwnProperty()` can be slower because it requires iterating through all properties of the object. **Library Purpose:** The library used in this benchmark is not explicitly mentioned, but it's likely that the JavaScript engine being tested provides support for these data structures. The `Set` and `Object` constructors are part of the standard JavaScript API, which means they are implemented by the JavaScript engine itself. **Special JS Feature/Syntax:** * **Random Number Generation:** The test case uses a custom function `makeid()` to generate random numbers. This is likely used to create unique identifiers for elements being added to the Set or object. **Other Alternatives:** If you're interested in exploring alternative data structures for testing, here are some options: * **Arrays:** Arrays can be used as a replacement for Sets and objects. However, they do not maintain uniqueness like Sets do. * **Maps:** Maps are another data structure that can be used to store key-value pairs, similar to objects. * **Set-like Objects:** Some JavaScript implementations provide set-like objects, such as the `Set` class in Node.js. In conclusion, this benchmark tests the performance of adding elements to Sets and objects, highlighting their differences in execution time. By using a custom function for generating unique identifiers, it simulates real-world scenarios where sets are used to maintain uniqueness.
Related benchmarks:
Spread vs Object.assign
Spread vs Object.assign
IE11 Object.entries
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object values: Object.entries loop for
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS for .. of
Comments
Confirm delete:
Do you really want to delete benchmark?