Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys.includes vs object.hasOwnProperty
(version: 0)
Comparing performance of:
Object.keys.includes vs object.hasOwnProperty
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {}; for (let i=0; i<100; i++) { obj[`${i*Math.random()}`] = i-(Math.random()*Math.random()); }
Tests:
Object.keys.includes
for (let i=0; i<100; i++) { if (Object.keys(obj).includes(`${Math.random()*Math.random()}`)) console.log(true); }
object.hasOwnProperty
for (let i=0; i<100; i++) { if (obj.hasOwnProperty(`${Math.random()*Math.random()}`)) console.log(true); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys.includes
object.hasOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Browser/OS:
Chrome 102 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys.includes
4864.1 Ops/sec
object.hasOwnProperty
20122.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested, compared, and its pros and cons. **Benchmark Definition** The benchmark definition represents two JavaScript microbenchmarks that compare the performance of `Object.keys.includes` and `object.hasOwnProperty`. These methods are used to check if a property exists in an object. **Script Preparation Code** The script preparation code creates an empty object `obj` with 100 properties, where each property is assigned a value between 0 and 1. This ensures that the benchmark tests the performance of both methods on a large number of properties. ```javascript var obj = {}; for (let i=0; i<100; i++) { obj[`${i*Math.random()}`] = i-(Math.random()*Math.random()); } ``` **What is being tested?** The benchmark tests the performance of `Object.keys.includes` and `object.hasOwnProperty` methods in two ways: 1. **Direct Property Access**: Both methods are called with a random property name, which is generated using the `Math.random()` function. 2. **Random Property Name**: The same method is called multiple times with different random property names. **Options Compared** The benchmark compares the performance of two options: A) `Object.keys.includes` B) `object.hasOwnProperty` **Pros and Cons** **Option A: Object.keys.includes** Pros: * Simplistic and straightforward implementation. * Uses a built-in method, which is generally faster than a custom implementation. Cons: * May not be as efficient as other methods for large objects or deep object trees. * Requires the property name to be included in the `Object.keys()` array, which can be slower than accessing a property directly. **Option B: object.hasOwnProperty** Pros: * More flexible and efficient for large objects or deep object trees. * Does not require the property name to be included in an array. Cons: * May be slower than `Object.keys.includes` due to the additional lookup. * Requires more CPU cycles, as it checks if the property exists using a separate method call. **Library Used** None of the benchmark tests use any external libraries. The methods being tested are built-in JavaScript methods. **Special JS Feature/Syntax** None of the test cases use any special JavaScript features or syntax. **Other Alternatives** If you want to explore alternative approaches, here are some options: * **Using `in` Operator**: Instead of using `Object.keys()` and `.includes()`, you could use the `in` operator to check if a property exists in an object. This approach is more efficient but may not be as straightforward. * **Using a Custom Implementation**: You could implement your own custom method for checking property existence, which might be faster or more efficient than using built-in methods. Keep in mind that these alternatives are not necessarily better or worse; they just offer different trade-offs and performance characteristics.
Related benchmarks:
Object.keys(object).includes(key) vs key in object
Fastest way to check if object is empty (for in vs.
For in vs Object.entries
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?