Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in with hasOwnProperty() vs object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
6 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 (obj.hasOwnProperty(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:
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
3.4 Ops/sec
Object.keys
2.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **What is being tested?** The benchmark measures the performance of two different approaches for iterating over an object: 1. **for-in**: This approach uses the `in` keyword to iterate over the object's properties, and then checks if each property is a direct property using the `hasOwnProperty()` method. 2. **Object.keys**: This approach uses the `Object.keys()` function to get an array of the object's property keys, which can be iterated over directly. **Options compared** The two approaches are being compared: * **for-in with hasOwnProperty()**: This is the traditional way of iterating over an object's properties in JavaScript. * **Object.keys**: A more modern and efficient approach to get an array of property keys. **Pros and Cons of each approach:** 1. **for-in with hasOwnProperty()**: * Pros: + Simple and easy to understand. + Works well for small objects. * Cons: + Can be slower due to the additional overhead of checking if a property is a direct property using `hasOwnProperty()`. 2. **Object.keys**: * Pros: + Faster and more efficient. + More concise and expressive code. * Cons: + Requires the use of a modern browser or Node.js, as it was introduced in ECMAScript 2015. **Library and syntax used** None, this benchmark is using only built-in JavaScript features and libraries. **Special JS feature or syntax** No special features or syntax are being tested in this benchmark. The focus is on comparing the performance of two simple approaches to iterate over an object's properties. **Other alternatives** If you want to use other methods to iterate over an object's properties, some alternatives could be: * Using a `for` loop with array-like indices (e.g., `obj[0]`, `obj[1]`, etc.) * Using a library like Lodash or Ramda for functional programming approaches * Using the `entries()` method and iterating over it directly However, these alternatives may not be relevant to this specific benchmark, which is focused on comparing two basic approaches using built-in JavaScript features.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty 2
in vs. hasOwnProperty
hasOwn vs hasOwnProperty vs typeof
Object.hasOwn vs 'in' performance v2
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?