Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
key in object vs object.keys find
(version: 0)
Comparing performance of:
key in object vs object.keys find
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
key in object
var key = 'e'; for (var i=10000; i > 0; i--) { if (key in obj) { console.log(key); } }
object.keys find
var key = 'e'; for (var i=10000; i > 0; i--) { if (Object.keys(obj).find((record) => record === key)) { console.log(key); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
key in object
object.keys find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
key in object
66.8 Ops/sec
object.keys find
63.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definitions and explanations. **Benchmark Overview** The test measures the performance difference between two approaches: 1. Using the `in` operator (`key in obj`) to check if a key exists in an object. 2. Using the `Object.keys()` method with the `find()` method to achieve the same result. **Options Compared** Both options are being compared, but they differ in how the comparison is performed: * Option 1: `key in obj` uses a simple property existence check, which means it checks if the key exists as a property of the object. This approach has a low overhead because it only requires accessing the property descriptor. * Option 2: `Object.keys(obj).find((record) => record === key)` uses an array method to get the keys of the object and then finds the first matching key using the `find()` method. This approach creates an array, iterates over it, and performs a strict equality check for each key. **Pros and Cons** * **Option 1 (`key in obj`):** + Pros: Low overhead, fast property existence checks. + Cons: May return true even if the value associated with the key is not what was expected (e.g., due to symbol or non-enumerable properties). * **Option 2 (`Object.keys(obj).find()`:** + Pros: More explicit and safe way to check for key existence, handles all properties including symbols and non-enumerable ones. + Cons: Higher overhead due to array creation, iteration, and method call. **Library and Purpose** The `Object.keys()` method is a built-in JavaScript method that returns an array of the object's own enumerable property names. The `find()` method is also a built-in JavaScript method that returns the first element in the array that satisfies the provided testing function. **Special JS Feature or Syntax** There are no special features or syntax used in these benchmarks. **Other Alternatives** For similar performance-critical code, you may want to consider: * Using `hasOwnProperty()` instead of `in` for property existence checks, as it is more efficient and explicit. * Using a library like Lodash's `keyIn()` function, which provides a faster and safer way to check for key existence. Keep in mind that the choice of approach depends on your specific use case and requirements. If you need to perform frequent or extensive property existence checks, using `Object.keys()` with `find()` might be a better option for safety and performance. However, if you're working with simple cases where you can rely on the `in` operator's behavior, it may be acceptable to use that approach for its low overhead.
Related benchmarks:
Object.keys vs Object.values
Object.keys() vs _.key()
key in object vs object.key
entries vs keys lookup
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?