Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.*.forEach vs Object.values for loop cached
(version: 0)
Comparing performance of:
For In vs Object values vs Object keys forEach vs Object entries forEach vs object values forEach vs cached length for loop
Created:
3 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(obj[key]); }
Object values
for(let value of Object.values(obj)){ console.log(value); }
Object keys forEach
Object.keys(obj).forEach(key => console.log(obj[key]));
Object entries forEach
Object.entries(obj).forEach(([key, value]) => console.log(key,'->',value));
object values forEach
Object.values(obj).forEach(value => console.log(value));
cached length for loop
const length = Object.keys(obj).lenght for (var i = 0; i < length ; i++) { console.log(Object.values(obj)[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
For In
Object values
Object keys forEach
Object entries forEach
object values forEach
cached length for loop
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, compared, and discussed. **Benchmark Overview** The benchmark measures the performance of different approaches for iterating over an object in JavaScript. Specifically, it compares the following methods: 1. `for...in` loop 2. `Object.values()` method 3. `Object.keys()` method with a `forEach` loop 4. `Object.entries()` method with a `forEach` loop 5. Caching the length of the object to avoid repeated lookups **Approaches Compared** The benchmark compares the performance of each approach in terms of executions per second (ExecutionsPerSecond). The approach with the highest value is considered faster. Here's a brief explanation of each approach: 1. **For...in loop**: This traditional way of iterating over an object uses the `in` operator to check if a property exists. 2. **Object.values()` method**: This method returns an array of values in the object, allowing for iteration using standard array methods like `forEach`. 3. **Object.keys()` method with forEach**: Similar to `Object.values()`, but iterates over the keys instead of values. 4. **Object.entries()` method with forEach**: Iterates over both keys and values as an array of [key, value] pairs. **Pros and Cons** Here are some pros and cons for each approach: 1. **For...in loop** * Pros: Simple to implement, works well with legacy code. * Cons: Can iterate over inherited properties, slower due to property lookup. 2. **Object.values()` method * Pros: Fast, modern way of iterating over values, easy to use. * Cons: Not suitable for objects with nested structures or non-string keys. 3. **Object.keys()` method with forEach** * Pros: Similar performance to `Object.values()`, works well with objects that don't have inherited properties. * Cons: Requires an additional method call (`Object.keys()`), slightly slower than `Object.values()` due to method overhead. 4. **Object.entries()` method with forEach** * Pros: Works well with nested structures and non-string keys, easy to implement. * Cons: Slowest of the four approaches due to the extra iteration step. **Library Usage** None of the provided methods rely on external libraries. **Special JavaScript Features/Syntax** There are no special features or syntax used in this benchmark. The focus is solely on comparing different iteration methods for objects. **Other Alternatives** If you're looking for alternative ways to iterate over an object, consider: 1. **Using a library like Lodash**: Offers utility functions for iterating over objects, including `_.forEachObject()`. 2. **Using a functional programming approach**: Utilize the `reduce()` method or other functional programming techniques to process the object's values. Keep in mind that this benchmark is focused on comparing different iteration methods, and the best choice ultimately depends on your specific use case and performance requirements.
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?