Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hasOwnProperty vs Object.keys (longer)
(version: 1)
Comparing performance of:
hasOwnProperty vs Object.keys
Created:
one year 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}; for (var i=0;i<1000;i++){ var key = i.toString(); props[i]=i; }
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:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
hasOwnProperty
12784.1 Ops/sec
Object.keys
16751.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Benchmark Overview The benchmark defined in the provided JSON is meant to compare two different methods of iterating over properties of an object in JavaScript. Specifically, it tests: 1. **Using `hasOwnProperty` for property checks** 2. **Using `Object.keys` to retrieve property keys** ### Description of Options Compared #### 1. **Using `hasOwnProperty`** - **Benchmark Code:** ```javascript for (var p in props) { if (Object.prototype.hasOwnProperty.call(props, p)) { props[p] = 2; } } ``` - **Summary of Approach:** - This approach uses a `for...in` loop to iterate over all enumerable properties of an object (`props`). - The `hasOwnProperty` method is employed to check whether the property belongs to the object directly and is not inherited. - **Pros:** - Can iterate over all enumerable properties, including those added after object creation. - Useful in cases where object prototypes are extended. - **Cons:** - Slower due to the need to perform a call to `hasOwnProperty` for each iteration. - The `for...in` loop also iterates over inherited properties unless filtered by `hasOwnProperty`, which can lead to potential bugs or performance issues if not handled. #### 2. **Using `Object.keys`** - **Benchmark Code:** ```javascript for (var k = Object.keys(props), l = k.length, i = 0; i < l; i++) { props[k[i]] = 2; } ``` - **Summary of Approach:** - This method leverages `Object.keys`, which returns an array of the object’s own enumerable property names. - It uses a traditional `for` loop to iterate through the array of keys. - **Pros:** - Typically faster than the `hasOwnProperty` method since it does not include the overhead of additional checks in each iteration. - Returns only the object's own properties, avoiding the potential pitfalls of inherited properties. - **Cons:** - Requires the creation of an array of keys, which uses additional memory. - If the object is very large, this could also introduce a performance hit due to array creation time. ### Library and Syntax Features Used In this benchmark, no specific external libraries are used—everything operates within native JavaScript functionality. The benchmark primarily relies on core language features such as objects and iteration constructs. ### Benchmark Results From the latest benchmark results, we observe that: - **`Object.keys`**: Executed at approximately **100,873** operations per second. - **`hasOwnProperty`**: Executed at approximately **46,483** operations per second. ### Considerations and Alternatives The benchmark clearly shows that using `Object.keys` is significantly faster than using `hasOwnProperty` with a `for...in` loop for checking object properties. **Other Alternatives:** 1. **`Object.entries`**: This would provide an array of key-value pairs for both own properties and could be used in a similar loop structure. 2. **`Map` Objects**: If you require a more efficient way to manage key-value pairs without the concerns of inheritance, using a `Map` object might be preferable. 3. **`forEach` method**: If you are leveraging arrays (after getting keys using `Object.keys`), you could also consider using the `.forEach()` method for clearer and more expressive code. Overall, the decision between these methods should take into account the specific requirements of your code (e.g., speed vs memory efficiency) and the context in which they are implemented.
Related benchmarks:
hasOwnProperty vs Object.keys
getOwnPropertyNames vs Object.keys
for-in vs object.keys (own props)
hasOwnProperty vs Object.keys to check whether an Object is empty
getOwnPropertyNames vs Object.keys - performance
for-in hasOwnProperty vs object.keys test vs for-in
getOwnPropertyNames vs Object.keys vs for ... in
getOwnPropertyNames() vs Object.keys() vs for ... in
hasOwnProperty vs Object.keys (longer and more options)
Comments
Confirm delete:
Do you really want to delete benchmark?