Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object length (for-in vs object.keys)
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
2 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 (let i=10000; i > 0; i--) { let i = 0; for (var key in obj) { i++ } console.log(i) }
Object.keys
for (let i=10000; i > 0; i--) { console.log(Object.keys(obj).length); }
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 120 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
6.6 Ops/sec
Object.keys
6.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares the performance of two approaches for iterating over an object's properties: the traditional `for-in` loop and the `Object.keys()` method. **Options Compared** Two options are compared: 1. **Traditional `for-in` loop**: This approach uses a traditional `for` loop with a manual index variable (`i`) to iterate over the object's properties. 2. **`Object.keys()` method**: This approach uses the `Object.keys()` method, which returns an array of property names in the specified object. **Pros and Cons** * **Traditional `for-in` loop**: + Pros: Can be more flexible and allow for manual iteration over specific properties or arrays. + Cons: May have performance issues due to the need for a manual index variable and potential type coercion. * **`Object.keys()` method**: + Pros: Is generally faster and more concise, as it uses an optimized array of property names. + Cons: May not be as flexible as the traditional `for-in` loop, as it only returns an array of property names. **Other Considerations** Both approaches have performance implications: * The `Object.keys()` method is typically faster because it avoids the need for a manual index variable and type coercion. * However, the traditional `for-in` loop can be optimized by using `Object.getOwnPropertyNames()` or `Object.keys()` with an array-like object (e.g., `{...}`), which reduces overhead. **Library Used** The benchmark uses the JavaScript `Object` and `Array` prototypes to create a sample object (`obj`) and retrieve its property names using `Object.keys()`. The `for-in` loop is implemented manually, without any external libraries or helpers. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. Both approaches only use standard JavaScript constructs (variables, data types, loops). **Other Alternatives** If you were to create a similar benchmark for other object iteration methods, some alternatives might include: * `Array.prototype.forEach()`: A more concise and modern way to iterate over arrays. * `for...of` loop: An older, yet still supported, way to iterate over arrays and objects using destructuring syntax. * `map()` and `reduce()` methods: Can be used to perform more complex operations on array-like objects. These alternatives might not directly compare with the traditional `for-in` loop or `Object.keys()` method but could provide additional insights into JavaScript's iteration capabilities.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
for-in vs for object.keys keys
Cached Object.keys() vs inline Object.keys()
Comments
Confirm delete:
Do you really want to delete benchmark?