Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.*.forEach vs Object.keys
(version: 0)
Comparing performance of:
For In vs Object values vs Object keys forEach vs Object entries forEach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = new Object() var keys = (new Array(10000)).fill(0).map((x, i) => { return i + 1 }) keys.forEach((x) => { obj['prop' + x] = x })
Tests:
For In
for (var key in obj) { console.log(key, obj[key]); }
Object values
for(let key of Object.keys(obj)){ console.log(key, obj[key]); }
Object keys forEach
Object.keys(obj).forEach(key => console.log(key, obj[key]));
Object entries forEach
Object.entries(obj).forEach(([key, value]) => console.log(key, value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
For In
Object values
Object keys forEach
Object entries forEach
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):
**Benchmark Overview** MeasureThat.net provides a platform for JavaScript microbenchmarks, allowing users to compare the performance of different approaches in various scenarios. The provided benchmark is designed to measure the performance of three ways to iterate over an object's properties: `for` loop with `in` keyword, `Object.keys()` method, and `forEach()` method. **Options Compared** The three options compared are: 1. **For In**: Using a traditional `for` loop with the `in` keyword to iterate over the object's properties. 2. **Object values**: Using the `Object.values()` method to get an array of the object's property values and then iterating over it using a `forEach()` loop. 3. **Object keys forEach**: Using the `Object.keys()` method to get an array of the object's property names and then iterating over it using a `forEach()` loop. **Pros and Cons** Here are some pros and cons of each approach: * **For In** + Pros: Simple, intuitive, and widely supported. + Cons: May be slower than other methods due to unnecessary iterations. * **Object values** + Pros: Efficient way to iterate over property values. Avoids unnecessary iterations. + Cons: Requires creating an array of property names beforehand, which can lead to additional memory usage. * **Object keys forEach** + Pros: Similar to `Object.values()` but allows iterating over property names and values in a single pass. + Cons: May be slower than other methods due to the additional iteration. **Library and Special JS Features** No specific libraries or special JavaScript features are used in this benchmark. However, it's worth noting that `Object.entries()` method is used in one of the test cases, which is a relatively new feature introduced in ECMAScript 2015 (ES6). **Other Considerations** * Memory usage: Creating an array of property names using `Object.keys()` or `Object.values()` can lead to additional memory usage. * Readability and maintainability: The choice of iteration method can impact code readability and maintainability. **Alternative Approaches** Some alternative approaches to iterating over objects include: * Using `for...in` loop with a custom iterator function * Using a library like Lodash or Underscore.js for functional programming-inspired iterators * Using a specialized object traversal library like Fastify's `Object.keys()` method It's worth noting that the performance differences between these approaches may vary depending on the specific use case and implementation details.
Related benchmarks:
For in vs Object.keys.forEach
For in vs Object.keys.forEach FixedForYaRetard
For in vs Object.keys.forEach 10000
For in vs For vs Object.keys.forEach
Some benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?