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
llama3.1:latest
, generated one year ago):
Let's break down the benchmark test case. **What is being tested?** The benchmark tests two different approaches to iterate over an object's properties: 1. Using a `for...in` loop (`for (var key in obj) { ... }`) 2. Using the `Object.keys()` method to get an array of keys, and then iterating over that array using a `for` loop (`for (var j = 0; j < keys.length; j++) { ... }`) **What options are compared?** The two test cases compare the performance of these two approaches: * "for-in" uses a `for...in` loop to iterate directly over the object's properties. * "Object.keys" uses the `Object.keys()` method to get an array of keys, and then iterates over that array using a `for` loop. **Pros and cons of each approach:** * **For...in**: This approach has some potential issues: + It can iterate over inherited properties (i.e., properties from the object's prototype chain), which might not be desirable. + The order of iteration is implementation-dependent, which means it may vary between browsers or environments. + In some cases, it can lead to performance issues due to the overhead of iterating over a potentially large number of properties. On the other hand, for...in has some benefits: + It's simple and easy to read. + It doesn't require creating an additional array of keys. * **Object.keys**: This approach avoids some of the issues with for...in: + It only iterates over own (non-inherited) properties. + The order of iteration is consistent, as it's based on the order in which the object's properties were created. + It can be more efficient than for...in, especially when dealing with large objects, since it doesn't require iterating over inherited properties. However, Object.keys has its own drawbacks: + It creates an additional array of keys, which may consume memory and affect performance. + The `Object.keys()` method is not supported in older browsers (before ECMAScript 2015). **Library or special JS feature:** No specific library or JavaScript feature is used in this benchmark. **Test preparation code:** The test case prepares an object (`obj`) with a large number of properties, and then runs each test case in a loop that iterates 10,000 times. This helps to isolate the performance differences between the two approaches. **Other alternatives:** Besides for...in and Object.keys, there are other ways to iterate over an object's properties: * **Object.entries()**: Introduced in ECMAScript 2015 (ES6), this method returns an array of [key, value] pairs. * **Object.values()**: Also introduced in ES6, this method returns an array of values for the object's own properties. * **Map.prototype.forEach()**: If you're working with maps instead of objects, you can use the `forEach()` method to iterate over key-value pairs. Note that these alternatives may have their own performance characteristics and trade-offs.
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?