Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys for-of
(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 };
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--) { for (var key of Object.keys(obj)) { console.log(key); } }
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.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Purpose:** MeasureThat.net is testing two approaches for iterating over an object in JavaScript: 1. **For-in loop**: This loop iterates over an object using its property names (i.e., keys). 2. **Object.keys() function**: This method returns an array of a given object's own enumerable property names. **Options being compared:** The benchmark is comparing the performance of these two approaches: * For-in loop * Object.keys() function **Pros and Cons:** 1. **For-in loop**: * Pros: + Can iterate over objects with non-enumerable properties (i.e., properties that are not included in the object's prototype chain). + Does not require the object to be in a specific state (e.g., not being modified by other scripts). * Cons: + May iterate over non-enumerable properties, which can lead to unexpected behavior. + Can be slower due to the overhead of property lookup and iteration. 2. **Object.keys() function**: * Pros: + Is a built-in method that is optimized for performance. + Does not require iterating over non-enumerable properties. * Cons: + Only iterates over enumerable properties, which may exclude some properties from the object. **Library usage:** None of the benchmark tests use any external libraries. The `Object.keys()` function is a built-in method in JavaScript. **Special JS features or syntax:** There are no special JavaScript features or syntax being tested in this benchmark. **Other considerations:** Keep in mind that this benchmark only compares two specific approaches for iterating over an object. Other factors, such as the object's size, complexity, and contents, may affect performance. **Alternative approaches:** Other approaches for iterating over an object in JavaScript include: * **For...of loop**: This is a newer iteration method introduced in ECMAScript 2015 (ES6). It can be used to iterate over objects using the `for...of` statement. * **Array.prototype.forEach()**: This method iterates over arrays and objects that support iterative protocols. However, it may not work as expected for all types of objects. In summary, this benchmark is testing the performance of two approaches for iterating over an object in JavaScript: the traditional For-in loop and the Object.keys() function. The results can help developers understand which approach to use depending on their specific use case.
Related benchmarks:
For in vs For of
for-in vs Object.keys()
Object.keys(obj)[0] vs for in
for-in vs for object.keys keys
for-in vs object keys map vs object keys loop
Comments
Confirm delete:
Do you really want to delete benchmark?