Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object: Iterate keys
(version: 0)
Iterate the keys of an object in a few different ways.
Comparing performance of:
for...of Object.keys() vs for...in !hasOwnProperty() vs for...in
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var x = {}, sum = 0; for (var i=0; i<20; ++i) { x[`k${i}`] = i; }
Tests:
for...of Object.keys()
for (var k of Object.keys(x)) sum += x[k];
for...in !hasOwnProperty()
for (var k in x) { if (!x.hasOwnProperty(k)) continue; sum += x[k]; }
for...in
for (var k in x) sum += x[k];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for...of Object.keys()
for...in !hasOwnProperty()
for...in
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 JSON represents a JavaScript microbenchmark test case on the website MeasureThat.net. In this benchmark, three different approaches are compared for iterating over the keys of an object: `for...of Object.keys()`, `for...in !hasOwnProperty()`, and `for...in`. **Approach 1: `for...of Object.keys()`** This approach uses the `Object.keys()` method to get an array of a given object's own enumerable property names, which can then be iterated over using a `for...of` loop. The benefits of this approach are: * It is concise and easy to read. * It avoids the need to manually check for properties that do not exist in the object. However, there are some potential drawbacks: * The performance might not be optimal since it involves calling an additional method (`Object.keys()`). * Some browsers may optimize `for...of` loops differently than others, which could affect the results. **Approach 2: `for...in !hasOwnProperty()`** This approach uses a combination of the `for...in` loop and the `!hasOwnProperty()` method to exclude properties that do not exist in the object. The benefits of this approach are: * It can be more efficient than the first approach since it avoids calling an additional method (`Object.keys()`). * However, it requires manual iteration over the array-like objects returned by `for...in`, which can be error-prone. The drawbacks of this approach include: * It is less concise and more prone to errors compared to the first approach. * Some browsers may not optimize `for...in` loops as effectively. **Approach 3: `for...in`** This approach uses a plain `for...in` loop to iterate over the object's properties. The benefits of this approach are: * It is straightforward and easy to understand. * However, it requires manual iteration over the array-like objects returned by `for...in`, which can be error-prone. The drawbacks of this approach include: * It is less efficient than the first two approaches since it involves more overhead due to the manual iteration. * Some browsers may optimize `for...in` loops differently than others, which could affect the results. It's worth noting that the JavaScript engine (V8) in modern browsers has been optimized for performance and can often achieve similar results using different methods. Additionally, some tests might have specific requirements or constraints that favor one approach over another. The library used in this benchmark is not explicitly mentioned, but it is likely that MeasureThat.net uses a combination of built-in JavaScript features and browser-specific optimizations to achieve accurate measurements. Special JS feature used in this test case is the `for...of` loop introduced in ECMAScript 2015 (ES6).
Related benchmarks:
Summing an object
Javascript iterate object keys
Object.entries vs Object.keys vs for...in
for in loop
Comments
Confirm delete:
Do you really want to delete benchmark?