Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in Object.hasOwn vs for-of Object.keys
(version: 0)
Comparing performance of:
for-in Object.hasOwn vs for-of Object.keys
Created:
2 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, 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:
for-in Object.hasOwn
for (let i = 10000; i > 0; i--) { for (const key in obj) { if (!Object.hasOwn(obj, key)) { continue } console.log(key); } }
for-of Object.keys
for (let i = 10000; i > 0; i--) { for (const key of Object.keys(obj)) { console.log(key) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in Object.hasOwn
for-of Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in Object.hasOwn
0.3 Ops/sec
for-of Object.keys
0.3 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 Overview** The benchmark compares two approaches for iterating over an object's properties: `Object.hasOwn` (an older method) versus `for...of` with `Object.keys()` (a modern method). **What is being tested?** In this specific benchmark, the test cases iterate over a large object (`obj`) and log each key to the console. The difference between the two approaches lies in how they access the object's properties. **Options compared:** 1. **`Object.hasOwn(obj, key)`**: This method checks if an object has a property with the given `key`. It returns `true` if the property exists and is not inherited from the prototype chain. 2. **For...of loop with `Object.keys()`**: This approach uses the `Object.keys()` method to get an array of the object's property names, and then iterates over this array using a for...of loop. **Pros and Cons:** * **`Object.hasOwn`:** + Pros: - Can be faster for smaller objects or when iterating over a subset of properties. - More readable code, as it explicitly checks for property existence. + Cons: - Can be slower for large objects due to the overhead of method calls and property lookup. - May not be as efficient when iterating over inherited properties. * **For...of loop with `Object.keys()`:** + Pros: - Generally faster and more efficient, especially for large objects. - Simplifies code by avoiding explicit property checks. + Cons: - Less readable code, as it relies on the internal implementation of `Object.keys()`. - May not work correctly if the object has non-enumerable properties. **Other considerations:** * **Iteration order**: The for...of loop with `Object.keys()` is generally more predictable and consistent in its iteration order compared to `Object.hasOwn`, which can return a subset of properties. * **Memory usage**: Both approaches have similar memory usage, as they only store the object's property names. **Library/dependencies:** None are explicitly mentioned in this benchmark. However, it's worth noting that `Object.keys()` is a built-in method and does not rely on any external libraries. No special JavaScript features or syntax are used in these test cases. **Other alternatives:** * For iterating over an object's properties, other approaches could include: + Using a for...in loop with the `in` operator (less efficient than `Object.keys()`). + Using the `for...of` loop with an array of property names (more efficient than using `Object.keys()`, but may require manual property name generation). + Using a library like Lodash or Underscore.js, which provide various utility functions for working with objects and arrays.
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
for-in vs for-in hasOwnProperty vs object.keys forEach
hasOwnProperty vs Object.keys to check whether an Object is empty
for-in with hasOwnProperty vs for-of over Object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?