Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in with hasOwnProperty checking vs object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
5 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 };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { if ({}.hasOwnProperty(obj, key)) { console.log(key); } } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => 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.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to iterate over an object in JavaScript: using `for-in` with a check for `hasOwnProperty`, and using `Object.keys`. **Options Compared** The two options are: 1. **For-in with hasOwnProperty checking**: This approach uses the `for-in` loop, which iterates over an object's properties using their property names as strings. The `hasOwnProperty` method is used to check if a property belongs to the object. 2. **Object.keys**: This approach uses the `Object.keys()` method, which returns an array of a given object's own enumerable property names. **Pros and Cons** **For-in with hasOwnProperty checking:** Pros: * More explicit control over iteration * Can be useful for iterating over objects that may have inherited properties from a prototype chain Cons: * Slower performance compared to `Object.keys` * Can lead to unexpected behavior if not used carefully (e.g., when dealing with prototype chains) **Object.keys:** Pros: * Faster performance compared to `for-in` with `hasOwnProperty` checking * More concise and expressive code Cons: * Less explicit control over iteration * May not work as expected for objects that have inherited properties from a prototype chain **Other Considerations** * The benchmark is run in Firefox 85 on a Linux desktop, which may affect the results. * The `var` keyword is used in both options, which can lead to variable hoisting and other issues in older browsers. **Library and Special JS Feature** There are no external libraries being used in this benchmark. However, the `for-in` loop uses a special JavaScript feature called "property iteration," which allows iterating over an object's properties using their property names as strings. **Benchmark Preparation Code** The preparation code creates an object `obj` with 10 properties ( `'a'` to `'j'`) and assigns each property a value of 1. This object is used for both test cases. **Individual Test Cases** Each test case defines a separate benchmark that measures the performance of one of the two options. The first test case uses the `for-in` loop with a check for `hasOwnProperty`, while the second test case uses `Object.keys`. The latest benchmark results show that the `Object.keys` approach is significantly faster than the `for-in` with `hasOwnProperty` checking approach. **Alternatives** Other approaches to iterate over an object in JavaScript include: * Using `forEach()` or `map()` * Using a `while` loop * Using a library like Lodash's `iteratee()` However, these alternatives may have different performance characteristics and trade-offs compared to the options being tested.
Related benchmarks:
undefined vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty 2
in vs. hasOwnProperty
hasOwn vs hasOwnProperty vs typeof
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?