Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys().includes() vs hasOwnProperty
(version: 0)
Comparing performance of:
HasOwnProperty vs Includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var props = {a:1,b:1,c:1,d:1,e:1,f:1,g:1,h:1,i:1,j:1,k:1,l:1,m:1,n:1,o:1,p:1,q:1,r:1,s:1,t:1,u:1,v:1,w:1,x:1,y:1,z:1};
Tests:
HasOwnProperty
props.hasOwnProperty('z')
Includes
Object.keys(props).includes('z')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
HasOwnProperty
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
HasOwnProperty
64959432.0 Ops/sec
Includes
15304830.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Purpose:** The goal of this benchmark is to compare the performance difference between two methods for checking if an object has a property: 1. `hasOwnProperty`: A method that checks if an object (usually the `this` context) has a property with a given key. 2. `includes`: A method that searches for a value in an array or object and returns true if found, or false otherwise. **Options Compared:** * `hasOwnProperty` * `includes` **Pros and Cons of Each Approach:** 1. **hasOwnProperty**: * Pros: + Native JavaScript method, so it's fast and efficient. + Works well for objects with a small number of properties. * Cons: + Only works on the `this` object, which might be unexpected behavior in some cases. + Can be slower than `includes` for large objects or arrays. 2. **includes**: * Pros: + Works well for both objects and arrays. + More flexible than `hasOwnProperty`, as it can search for values in any type of collection. * Cons: + Introduced in ECMAScript 2019, so older browsers might not support it. + Might be slower than `hasOwnProperty` for very large collections. **Library Usage:** There is no explicit library used in this benchmark. The methods are part of the standard JavaScript language and are built-in to most modern browsers. **Special JS Features or Syntax:** There is no special JavaScript feature or syntax introduced in this benchmark. It only uses native methods and basic syntax for object property checks. **Alternative Approaches:** * For very large objects or arrays, you might want to consider using a more efficient data structure, such as a hash table or a binary search tree. * For objects with a small number of properties, `hasOwnProperty` is still a good choice due to its native performance and simplicity. * If you need to support older browsers that don't have the `includes` method, you might consider using a polyfill or a work-around like using a library that provides similar functionality. In summary, this benchmark compares the performance of two methods for checking object property existence: `hasOwnProperty` and `includes`. While `hasOwnProperty` is a native method with good performance for small objects, `includes` offers more flexibility but might be slower in certain cases.
Related benchmarks:
hasOwnProperty vs Object.keys
hasOwnProperty vs Object.keys to check whether an Object is empty
isContextEqual vs fast-deep-equal
Strict isContextEqual vs fast-deep-equal
Comments
Confirm delete:
Do you really want to delete benchmark?