Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..in vs for..loop with 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, '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, 'aa': 1, 'ba': 1, 'ca': 1, 'da': 1, 'ea': 1, 'fa': 1, 'ga': 1, 'ha': 1, 'ia': 1, 'ja': 1, 'ka': 1, 'la': 1, 'ma': 1, 'na': 1, 'oa': 1, 'pa': 1, 'qa': 1, 'ra': 1, 'sa': 1, 'ta': 1, 'ua': 1, 'va': 1, 'wa': 1, 'xa': 1, 'ya': 1, 'za': 1, 'ab': 1, 'bb': 1, 'cb': 1, 'db': 1, 'eb': 1, 'fb': 1, 'gb': 1, 'hb': 1, 'ib': 1, 'jb': 1, 'kb': 1, 'lb': 1, 'mb': 1, 'nb': 1, 'ob': 1, 'pb': 1, 'qb': 1, 'rb': 1, 'sb': 1, 'tb': 1, 'ub': 1, 'vb': 1, 'wb': 1, 'xb': 1, 'yb': 1, 'zb': 1 };
Tests:
for in
for (let i=100; i > 0; i--) { for (const key in obj) { if (obj.hasOwnProperty(key)) { console.log(key); } } }
object keys
var keys = Object.keys(obj); for (var i=100; i > 0; i--) { for (var j = 0; j < keys.length; j ++) { console.log(keys[j]); } }
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.1:latest
, generated one year ago):
Let's break down what's being tested here. **Benchmark Description** The benchmark is comparing the performance of two different ways to iterate over an object's properties: `for..in` and using `Object.keys()`. **Test Case 1: for..in** This test case uses a `for..in` loop to iterate over the properties of the `obj` object. The loop iterates from 100 down to 0, and inside the loop, it checks if each property belongs to the object using `hasOwnProperty()`. If it does, it logs the property name to the console. **Test Case 2: Object.keys()** This test case uses the `Object.keys()` method to get an array of the object's properties. Then, it iterates over this array using a traditional `for` loop, logging each property name to the console. **Library Used** No external libraries are used in these test cases. **JavaScript Feature/Syntax Used** No special JavaScript features or syntax are being tested here. **Options Compared** The two options being compared are: 1. Using a `for..in` loop to iterate over an object's properties. 2. Using `Object.keys()` to get an array of the object's properties, and then iterating over this array using a traditional `for` loop. **Pros/Cons** * **For..in**: Pros: concise syntax, easy to use. Cons: can be slow for large objects or arrays because it iterates over inherited properties as well. * **Object.keys()**: Pros: fast and efficient, only returns own property names. Cons: requires an additional method call. **Other Considerations** In modern JavaScript, `for..in` is generally discouraged in favor of using `Object.keys()` or other methods that provide more control over iteration. However, for small objects or simple use cases, `for..in` can still be a convenient choice. **Alternatives** If you need to iterate over an object's properties and don't care about performance, you could also use: * `Object.entries()`: Returns an array of key-value pairs for the object. * A traditional `for` loop with a manual check for each property: While this approach is not shown in these test cases, it can be more efficient than `for..in` but less convenient. In summary, this benchmark compares the performance of two common ways to iterate over an object's properties in JavaScript: using `for..in` and using `Object.keys()`. The results show that `Object.keys()` is slightly faster than `for..in` in this particular test case.
Related benchmarks:
for-in vs object.keys with for loop large obj fixed
for-in vs object.keys with for
for..in vs for loop with Object.keys
for..in vs for loop using Object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?