Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hasOwnProperty vs Object.keys
(version: 0)
Comparing performance of:
hasOwnProperty vs Object.keys
Created:
8 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
for (var p in props) { if (Object.prototype.hasOwnProperty.call(props, p)) { props[p] = 2; } }
Object.keys
for (var k=Object.keys(props), l=k.length, i=0; i<l; i++) { props[k[i]] = 2; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
hasOwnProperty
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
hasOwnProperty
3461594.2 Ops/sec
Object.keys
5198490.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, compared, and the pros and cons of different approaches. **Benchmark Description** The benchmark measures the performance difference between two JavaScript methods: `hasOwnProperty` and `Object.keys`. Both methods are used to check if a property exists in an object. However, they work differently: * `hasOwnProperty` checks if the specified property exists in the object's own property set (i.e., not inherited from its prototype chain). It returns `true` if the property is found and `false` otherwise. * `Object.keys` returns an array of the object's own enumerable property names, which can then be used to check if a specific property exists. **Script Preparation Code** The script preparation code defines an object `props` with 31 properties (a-z) initialized with a value of 1. This object is used as the test subject for both benchmark methods. **Html Preparation Code** There is no HTML preparation code provided, which means that only the JavaScript execution time is being measured. **Benchmark Test Cases** There are two individual test cases: 1. **"hasOwnProperty"`** The benchmark definition script uses a `for` loop to iterate over the object's properties using `Object.prototype.hasOwnProperty.call(props, p)`. If the property exists, it assigns a value of 2 to the property. 2. **"Object.keys"`** The benchmark definition script uses another `for` loop to iterate over the array of property names returned by `Object.keys(props)`. It then assigns a value of 2 to each property. **Benchmark Results** The latest benchmark results show that: * `Object.keys` is slightly faster than `hasOwnProperty`, with an execution rate of approximately 1394783.75 executions per second (FPS). * The test was run on Firefox 129 on a Windows desktop device. **Pros and Cons of Different Approaches** 1. **Using `hasOwnProperty`**: * Pros: Simple, well-established method for checking property existence. * Cons: May be slower due to the overhead of object lookup and property access. 2. **Using `Object.keys`**: * Pros: Can be faster since it uses an array of property names, which can be iterated over more efficiently. * Cons: Requires additional memory allocation and iteration. **Library and Special JS Features** No libraries are used in this benchmark, but it does utilize the built-in `Object.prototype.hasOwnProperty.call()` method. There are no special JavaScript features or syntaxes being tested in this benchmark. **Alternative Approaches** 1. **Using a custom iterator**: Instead of using `hasOwnProperty` or `Object.keys`, you could implement a custom iterator to traverse the object's properties. 2. **Using a library function**: If you're working with large datasets, you might consider using a library function like Lodash's `_.includes()` to check for property existence. Keep in mind that these alternative approaches would likely have different performance characteristics and may not be suitable for all use cases. In conclusion, the benchmark provides valuable insights into the relative performance of two JavaScript methods for checking property existence. While both methods have their pros and cons, using `Object.keys` appears to be slightly faster in this particular case.
Related benchmarks:
undefined vs. hasOwnProperty
undefined vs. hasOwnProperty2
instanceof vs hasOwnProperty2
in vs. hasOwnProperty
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?