Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object iterating
(version: 0)
Comparing performance of:
forEach vs For Of vs ForIN vs Object. entries
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "subTitle": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }
Tests:
forEach
items. forEach(item => console.log(item))
For Of
for (const item of items) {console.log(item)}
ForIN
for (const item in items) { console.log(item) }
Object. entries
Object.entries(items).map(item => { console.log(item) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
For Of
ForIN
Object. entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
0.0 Ops/sec
For Of
0.0 Ops/sec
ForIN
84479.9 Ops/sec
Object. entries
59420.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to measure the performance of different JavaScript iteration methods: `forEach`, `For Of`, and `For IN`. The benchmark creates an object with a specified number of items, which is not actually used in this case. Instead, each test case iterates over the `items` object and logs its values to the console. **Iteration Methods** Here's a brief explanation of each iteration method: 1. **forEach**: This method was introduced in ECMAScript 2015 (ES6). It allows you to iterate over an array-like object (in this case, the `items` object) and execute a callback function for each item. 2. **For Of**: This method was also introduced in ES6. It's similar to `forEach`, but it only works with arrays, not array-like objects. 3. **For IN**: This is an older iteration method that uses the `in` operator to iterate over the object's properties. **Options Compared** The benchmark compares the performance of these three iteration methods: * **forEach**: The standard ES6 iteration method. * **For Of**: A more modern and efficient way to iterate over arrays, introduced in ES6. * **For IN**: An older, less efficient way to iterate over objects' properties. **Pros and Cons** Here are some pros and cons for each option: 1. **forEach**: * Pros: Easy to use, works with both arrays and array-like objects, can be used with callbacks or arrow functions. * Cons: Less efficient than `For Of` for arrays. 2. **For Of**: * Pros: Efficient, modern, and easy to use. Only works with arrays, so it's not a good choice for iterating over object properties. * Cons: Requires the array to be declared using `const` or `let`. 3. **For IN**: * Pros: Works with both objects and arrays (in older browsers). * Cons: Less efficient than `forEach` and `For Of`, can lead to issues if not used correctly. **Library and Special Features** In this benchmark, the following libraries are used: * None Special features like ES6 syntax or browser-specific features are not mentioned in the provided code. However, it's worth noting that some browsers may have different performance characteristics due to their engine implementations. **Other Alternatives** For iterating over objects' properties, you can also use `Object.keys()`, `Object.values()`, and `Object.entries()` methods, which were introduced in ES6. These methods provide more control over the iteration process and are generally faster than using a loop with `in`. Additionally, if you need to iterate over an array or array-like object, you can consider using libraries like Lodash or Ramda, which provide additional utility functions for working with arrays. In conclusion, this benchmark provides a good overview of the performance differences between different JavaScript iteration methods. By understanding the pros and cons of each option, developers can choose the most efficient approach for their specific use cases.
Related benchmarks:
Array.slice vs Array.concat
Array iteration
Array iteration V1
Stuffatleast6characters
Comments
Confirm delete:
Do you really want to delete benchmark?