Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object hasOwnProperty vs Set has
(version: 0)
Comparing performance of:
Object Lookup vs Set Lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = []; for (let i = 0; i < 5000; i += 1) { data.push(i.toString()); } var obj = data.reduce((p, c) => { p[c] = true; return p; }, {}); var set = new Set(data);
Tests:
Object Lookup
obj.hasOwnProperty('123')
Set Lookup
set.has('123')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object Lookup
Set Lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object Lookup
86618800.0 Ops/sec
Set Lookup
142918784.0 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. **Benchmark Definition JSON** The provided benchmark definition JSON contains information about the test case. Here's a breakdown: * `Name`: The name of the benchmark, which is "Object hasOwnProperty vs Set has". * `Description`: An empty description field, indicating that no detailed explanation or context is needed for this benchmark. * `Script Preparation Code`: A script that creates an array `data` with 5000 elements, converts each element to a string using `toString()`, and then reduces the array to create an object `obj`. The object has properties equal to the values in the data array. Another set `set` is created from the same data array. * `Html Preparation Code`: An empty field, indicating that no HTML-specific setup or teardown is required for this benchmark. **Test Cases** The test cases are individual tests within the benchmark. There are two test cases: 1. **Object Lookup**: Tests the performance of checking if a property exists in an object using `obj.hasOwnProperty('123')`. 2. **Set Lookup**: Tests the performance of checking if an element exists in a set using `set.has('123')`. **Comparison of Options** The test case uses two approaches to check for the existence of properties: * Object lookup (`obj.hasOwnProperty('123')`): This method checks if the property `123` exists directly on the object. * Set lookup (`set.has('123')`): This method checks if the element `'123'` exists in the set. **Pros and Cons** Here are some pros and cons of each approach: * Object lookup: + Pros: - Simple and straightforward implementation. - Fast, since it only involves a single property access. + Cons: - May not be as efficient for large datasets or complex objects, since it requires traversing the object's prototype chain to find the property. * Set lookup: + Pros: - Faster for large datasets, since sets are optimized for fast membership testing. - More flexible, as sets can store any type of value, not just strings. + Cons: - May be slower than object lookup for small or simple objects, due to the overhead of creating and managing a set. **Library: `Set`** The `Set` library is used in this benchmark to create a set from the data array. A set is a collection of unique values that allows fast membership testing using the `has()` method. In this context, the set is used to store the elements of the data array, allowing for efficient lookup and removal. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The code uses standard JavaScript language features, such as arrays, objects, sets, and loops. **Alternatives** Other alternatives for checking property existence could be: * Using the `in` operator to check if a property exists in an object. * Using a library like Lodash's `has()` function, which provides a more robust and flexible way of checking property existence. * Using a data structure like a trie or a prefix tree, which can provide efficient membership testing for certain types of data. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
JavaScript reduce vs Object.assign performance v2
Object.fromEntries vs reduce vs property assignment
Object.fromEntries vs reduce vs property assignment vs Map
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce (small dataset)
Comments
Confirm delete:
Do you really want to delete benchmark?