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
gemma2:9b
, generated one year ago):
This benchmark compares two ways to iterate over the properties of a JavaScript object: using `for...in` and using `Object.keys`. **`for...in`:** * **Description:** This traditional method iterates through each property name (key) in an object. The `hasOwnProperty()` check is included to avoid iterating over inherited properties, which might not be desired. * **Pros:** More concise syntax, can work with any object regardless of whether it has a defined keys array. * **Cons:** Slower than `Object.keys` in modern JavaScript engines because it relies on implicit iteration and property descriptor checks. Can also be more complex to understand for beginners. **`Object.keys`:** * **Description:** This method returns an array containing all the enumerable property names of an object. You then iterate over this array using a `for` loop. * **Pros:** Generally faster than `for...in` because it avoids unnecessary checks and provides a predictable iteration order. * **Cons:** Requires an extra step to call `Object.keys()` and iterate over the returned array, which can be slightly less concise. **Benchmark Results:** The benchmark results show that `Object.keys` is consistently faster than `for...in` in this specific case. This aligns with general performance expectations. **Alternatives:** * **Modern JavaScript Iterators:** For more advanced scenarios and potentially better performance, you could consider using modern iterators and the `for...of` loop to iterate over the object's values. ```javascript const obj = { a: 1, b: 2 }; for (const value of Object.values(obj)) { console.log(value); } ``` * **Libraries:** While this benchmark doesn't use libraries, there are specific JavaScript libraries designed for optimizing object manipulation and iteration if your use case demands it.
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?