Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.*.forEach vs Object.values with equal usage
(version: 0)
Comparing performance of:
For In vs Object values vs Object keys forEach vs Object entries forEach vs object values 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 (const key in obj) { console.log(key, obj[key]); }
Object values
for(let value of Object.values(obj)){ console.log(value); }
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));
object values forEach
Object.values(obj).forEach(value => console.log(value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
For In
Object values
Object keys forEach
Object entries forEach
object values 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):
Let's dive into the benchmark and explore what's being tested. **Benchmark Definition** The benchmark definition is a JSON object that defines the test case. It contains a script preparation code, which sets up an object with 10 properties ( keys ) using the `for...in` loop. The prepared HTML is empty, indicating that no HTML-related tests are performed. In this benchmark, four different approaches are being compared: 1. **For In**: This approach uses the `for...in` loop to iterate over the object's properties. 2. **Object values**: This approach uses `Object.values()` to get an array of the object's property values. 3. **Object keys forEach**: This approach uses `Object.keys()` and `forEach()` to iterate over the object's property names. 4. **Object entries forEach**: This approach uses `Object.entries()` and `forEach()` to iterate over the object's key-value pairs. **Options Compared** The four approaches are compared in terms of their performance, specifically: * How many executions per second (ExecutionsPerSecond) each approach can achieve. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **For In**: * Pros: Simple and widely supported; no additional library or module needed. * Cons: May not be as efficient as other approaches, especially for large objects. 2. **Object values**: * Pros: Efficient and widely supported; uses the `Object.values()` method, which is relatively fast. * Cons: Requires a modern browser that supports `Object.values()`. 3. **Object keys forEach**: * Pros: Fast and efficient; uses `forEach()` to iterate over the property names. * Cons: May not be as widely supported as other approaches, especially older browsers. 4. **Object entries forEach**: * Pros: Efficient and unique approach that iterates over key-value pairs. * Cons: Less widely supported than other approaches; requires a modern browser that supports `Object.entries()`. **Libraries and Features** None of the approaches rely on specific libraries or modules, except for: * **Object.values()**: This method is part of the ECMAScript standard, so all modern browsers support it. * **Object.keys()**: Also part of the ECMAScript standard, but some older browsers may not support it. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark. All approaches use standard ECMAScript syntax. **Alternatives** Other alternatives to these approaches might include: * Using a library like `lodash` that provides an efficient way to iterate over objects. * Using a framework like React or Angular, which provide built-in support for iterating over objects and arrays. * Implementing custom iteration logic using a different approach, such as using a custom iterator or a loop. Keep in mind that the best approach will depend on the specific use case and requirements. This benchmark aims to provide a general comparison of four common approaches to iterating over objects in JavaScript.
Related benchmarks:
For in vs Object.keys.forEach
For in vs Object.keys.forEach FixedForYaRetard
For in vs Object.keys.forEach 10000
Some benchmark
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?