Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for..of object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys vs for..of Object.keys
Created:
4 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, };
Tests:
for-in
for (let i=10000; i > 0; i--) { for (const key in obj) { console.log(key); } }
Object.keys
for (let i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(key)); }
for..of Object.keys
for (let i=10000; i > 0; i--) { for (const key of Object.keys(obj)) { console.log(key); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
for..of 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.2:3b
, generated one year ago):
The provided benchmark measures the performance of three different ways to iterate over the keys of an object in JavaScript: using `for-in`, `for..of` with `Object.keys()`, and using `Object.keys()` directly. **Options being compared:** 1. **For-in**: This method uses a traditional for loop that iterates over the properties of an object, including inherited ones. 2. **For..of** with `Object.keys()`: This method uses a newer syntax to iterate over the keys of an object and uses the `Object.keys()` function to get an array of those keys. 3. **Object.keys() directly**: This method only uses the `Object.keys()` function without any iteration mechanism. **Pros and Cons:** 1. **For-in**: * Pros: + Works with all versions of JavaScript, including older ones. + Does not require an array or iterator object. * Cons: + Iterates over inherited properties as well, which may not be desirable in some cases. + Can be slower due to the overhead of iterating over a potentially large number of inherited properties. 2. **For..of** with `Object.keys()**: * Pros: + More modern and efficient way to iterate over object keys. + Returns only the enumerable keys of the object, excluding inherited ones. + Can be faster due to the optimized iteration mechanism in newer JavaScript versions. * Cons: + Requires an array or iterator object, which may incur additional overhead. + May not work in older JavaScript versions that do not support this syntax. 3. **Object.keys() directly**: * Pros: + Fastest way to get an array of keys, since it uses a built-in function optimized for performance. * Cons: + Does not provide any iteration mechanism and will throw an error if used without an iterator or loop. **Library and Special JS Feature:** None mentioned in the provided benchmark. **Other Considerations:** When choosing between these options, consider the following factors: 1. **Version compatibility**: If you need to support older JavaScript versions, `for-in` may be a safer choice. 2. **Performance**: For large objects or high-performance applications, `For..of` with `Object.keys()` is likely to be the fastest option due to its optimized iteration mechanism. 3. **Readability and maintainability**: If you prioritize readability and maintainability, consider using `for...of` with `Object.keys()`, as it provides a clear and concise way to iterate over object keys. **Alternatives:** Other alternatives for iterating over object keys include: 1. Using a traditional for loop with the `in` operator (e.g., `for (key in obj) { ... }`) 2. Using the `forEach()` method on an array of keys (e.g., `Object.keys(obj).forEach(key => { ... })`) 3. Using a library like Lodash, which provides optimized iteration functions for arrays and objects. Keep in mind that these alternatives may have different performance characteristics or trade-offs with respect to readability and maintainability.
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 using Object.keys
Object.keys vs for-in
Comments
Confirm delete:
Do you really want to delete benchmark?