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()]=""; 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:
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):
I'd be happy to help you understand what's being tested in this JavaScript microbenchmark. **Overview** The benchmark measures the performance difference between using a `Set` data structure and an object (`Object`) to store unique identifiers. The test creates 5 random strings (each 5 characters long) and adds them to either a `Set` or an object, then checks if each string is present in the collection. **Options Compared** There are two options being compared: 1. **Set**: A `Set` is a built-in JavaScript data structure that stores unique values. It provides fast lookup, insertion, and deletion operations. 2. **Object**: An object is a general-purpose data structure that can store arbitrary key-value pairs. In this test, the strings are used as keys. **Pros and Cons of Each Approach** **Set** Pros: * Fast lookups: `Set` uses a hash table to store elements, making lookups constant-time (O(1)). * Efficient insertion and deletion: `Set` can add or remove elements in O(1) time. * Unique values: `Set` ensures that each value is unique. Cons: * Limited functionality: `Set` only provides basic operations; no methods for adding multiple values at once, etc. **Object** Pros: * Flexible data structure: Objects can store arbitrary key-value pairs and have more complex relationships between keys. * Methods available: Objects provide various methods for manipulating the collection (e.g., `forEach`, `map`, `reduce`). Cons: * Slower lookups: Object lookups are slower than `Set` due to the need to traverse the property chain. * More memory usage: Objects can lead to more memory usage due to the storage of multiple properties. **Library and Special JS Feature** In this benchmark, there is no explicit library or special JavaScript feature being used. However, it's worth noting that using `Set` has become a common pattern in modern web development, especially when dealing with unique identifiers or sets of data. **Benchmark Preparation Code** The script preparation code creates two variables: `set` and `obj`, both initialized as empty collections (i.e., `Set` and `Object`). The `makeid()` function generates 5 random strings, which are then added to either the `set` or `obj`. **Individual Test Cases** Each test case consists of a single benchmark definition: * **Set**: `set.add(makeid()); set.has(makeid())` - adds the generated string to the set and checks if it's present. * **Object**: `obj[makeid()] = ""; obj.hasOwnProperty(makeid())` - adds the generated string as a property in the object with an empty value, then checks if the property exists. **Benchmark Results** The latest benchmark results show the execution time per second for each test case: * **Set**: 173716.40625 executions/second * **Object**: 156727.203125 executions/second These results indicate that the `Set` approach is slightly faster than the object approach in this specific benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, some options might include: * Using a `Map` (another built-in JavaScript data structure) instead of an object. * Implementing your own custom data structure or algorithm for storing unique values. * Utilizing modern JavaScript features like WebAssembly or async/await to optimize performance. Keep in mind that these alternatives may not be relevant to the specific requirements of this benchmark, and their results might differ.
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?