Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys with for loop large obj fixed
(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 (var i=10000; i > 0; i--) { for (var key in obj) { console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { var keys = Object.keys(obj); 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 large JavaScript object: **1. `for...in` loop:** This is a traditional way to iterate over an object's enumerable properties. The code snippet for this test uses nested loops – one for decrementing a counter and another using `for...in` to iterate through each property name in the object. * **Pros:** Simple, often easier to understand initially. * **Cons:** Can be slower than other methods, especially with large objects, because it iterates over all properties regardless of their enumerability status. It can also be unpredictable as the order of iteration is not guaranteed. **2. `Object.keys()`:** This method returns an array containing all enumerable property names of an object. The code snippet for this test uses a `for` loop to iterate through the resulting array. * **Pros:** Faster and more predictable than `for...in`, as it avoids unnecessary iterations and provides a consistent order of iteration (alphabetical). * **Cons:** Requires an extra step to retrieve the property names into an array, which can be slightly more verbose. **Other considerations:** * **Object size:** The impact of these differences is more pronounced with larger objects. For small objects, the performance difference may be negligible. * **Specific use case:** If you need specific properties or are dealing with a complex object structure, there might be better ways to iterate than either `for...in` or `Object.keys()`. **Alternatives:** * Using libraries like Lodash for more specialized iteration functions. Let me know if you have any other questions!
Related benchmarks:
for-in vs object.keys with for
for..in vs for loop with Object.keys
for..in vs for loop using Object.keys
Object.keys vs for-in
Comments
Confirm delete:
Do you really want to delete benchmark?