Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys(object).includes(key) vs key in object
(version: 0)
Which is faster: Object.keys(object).includes(key) vs key in object
Comparing performance of:
Object.keys(object).includes(key) vs key in object
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = (new Array(1000)).fill(null).reduce((prev, newVal) => {prev[Math.random() + ''] = Math.random() + ''; return prev; }, { sausage: 'tst' });
Tests:
Object.keys(object).includes(key)
Object.keys(obj).includes('sausage')
key in object
'sausage' in obj
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys(object).includes(key)
key in 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):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two different approaches for checking if a key exists in an object: `Object.keys(object).includes(key)` versus `key in object`. **Options Compared** Two options are being compared: 1. **`Object.keys(object).includes(key)`**: This approach uses the `Object.keys()` method to get an array of the object's own enumerable property names, and then checks if the desired key is included in that array using the `.includes()` method. 2. **`key in object`**: This approach uses the `in` operator to check if the desired key exists as a property of the object. **Pros and Cons** * **`Object.keys(object).includes(key)`**: + Pros: This approach is more explicit and readable, as it clearly shows what's being done. It also allows for additional error checking or handling. + Cons: This approach can be slower than `key in object`, especially for large objects, since it creates an array of property names and then checks if the key is in that array. * **`key in object`**: + Pros: This approach is generally faster than `Object.keys(object).includes(key)`, as it only requires a single lookup operation. + Cons: The performance difference may be negligible for small objects, but can add up for larger ones. **Library/Function Used** The benchmark uses the `Array.prototype.fill()` and `Array.prototype.reduce()` methods to create an object with 1000 properties named "sausage" (although not all of them are actually used in this specific benchmark). **Special JS Feature/Syntax** No special JavaScript features or syntax are being tested in this benchmark. **Other Considerations** The benchmark is designed to be lightweight and easy to understand, making it accessible to a wide range of developers. The use of a simple object with 1000 properties makes it easy to understand the basic idea behind each approach. **Alternative Approaches** If you wanted to add more approaches to the benchmark, here are some alternatives: * Using `Object.prototype.hasOwnProperty.call()` instead of `key in object` * Using a library like Lodash's `hasOwnProperty()` function * Using a custom implementation that uses bit manipulation or other low-level optimization techniques These additional approaches would require modifying the script preparation code to include the necessary imports and implementations.
Related benchmarks:
array includes (worst case) vs object key
array includes vs object[key] vs key in object
array includes vs object key -1
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?