Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
NOT broken set.has vs obj key lookup
(version: 0)
Everyone else's was crap
Comparing performance of:
Find unique by obj hash key vs Find by set.has
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript"> const objHashTest = { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1, g: 1, h: 1, i: 1, j: 1, k: 1, l: 1, }; const setTest = new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']) </script>
Tests:
Find unique by obj hash key
if (objHashTest['a']) { } if (objHashTest['d']) { } if (objHashTest['g']) { } if (objHashTest['l']) { }
Find by set.has
if (setTest.has('a')) { } if (setTest.has('d')) { } if (setTest.has('g')) { } if (setTest.has('l')) { }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Find unique by obj hash key
Find by set.has
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'll break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is testing two approaches to find specific values in an object or set: 1. **Object Hash Key Lookup**: This approach uses the bracket notation (`objHashTest['a']`) to access the `a` property of the `objHashTest` object. 2. **Set Has Method**: This approach uses the `has()` method of the `setTest` Set object to check if a specific value is present in the set. **Comparison** The benchmark compares the performance of these two approaches on large datasets: * `Find unique by obj hash key` * `Find by set.has` **Pros and Cons** **Object Hash Key Lookup** Pros: * Fast access to properties using bracket notation * Can be used for arbitrary property lookups Cons: * May not work as expected if the property name is a reserved word or starts with a digit (due to JavaScript's property access rules) * Can lead to slower performance due to string lookup and indexing **Set Has Method** Pros: * Fast and efficient method for checking set membership * Handles arbitrary values, including strings, numbers, and objects Cons: * May have slower performance compared to bracket notation due to hashing and iteration * Not designed for property lookups; only for set membership checks **Library: Set** The `Set` object is a built-in JavaScript data structure that stores unique values. It provides the `has()` method to check if a value is present in the set. **Special JS Feature/Syntax: None mentioned** **Benchmark Preparation Code and Execution Results** The benchmark preparation code defines an object `objHashTest` with 13 properties, each containing the value `1`. A `Set` object `setTest` is created from an array of property names. The benchmark execution results show that: * `Find by set.has` has a higher number of executions per second (3237187) compared to `Find unique by obj hash key` (3172416). * The Chrome 107 browser and Linux operating system are used for the execution. **Other Alternatives** If you need to perform property lookups or arbitrary value checks, you can consider using other methods: * **Array.prototype.includes()**: A method that allows you to check if a value is present in an array. * **WeakMap**: A data structure that stores key-value pairs with weak references to keys. It's more efficient than `Set` for property lookups. * **Binary Search**: An algorithm that can be used to find specific values in sorted arrays or objects. Keep in mind that the choice of method depends on your specific use case, performance requirements, and language-specific features.
Related benchmarks:
Compare check existing on Object / Set / Map
set.has(key) vs. Boolean(obj[key])
set.has vs. Object key lookup for real without bang bang
set.has vs. Object key
set.has vs. Object key lookup2
Comments
Confirm delete:
Do you really want to delete benchmark?