Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object iteration methods kate and waller edition
(version: 0)
Comparing performance of:
For of vs forEachKey
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
window.obj = Object.fromEntries( new Array(10000) .fill(null) .map( _none => [ Math.random().toString(36).substr(2, 5), _.random(20) ] ) )
Tests:
For of
let res = {} for (let [key, value] of Object.entries(obj)) { res[key] = value * 2 }
forEachKey
Object.keys(obj).forEach((key) => { obj[key] = obj[key] * 2 })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For of
forEachKey
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 definition and test cases to understand what is being tested. **Benchmark Definition:** The provided `Script Preparation Code` creates an object `obj` with 10,000 key-value pairs, where each value is a random string followed by a random integer between 0 and 19. The `Html Preparation Code` includes the Lodash library, which provides utility functions for JavaScript. **Test Cases:** There are two test cases: 1. **For of**: This benchmark uses the `for...of` loop to iterate over the key-value pairs of the `obj` object. In each iteration, it calculates a new value by multiplying the original value with 2. 2. **forEachKey**: This benchmark uses the `forEach()` method on the `Object.keys()` array of the `obj` object. It iterates over the keys of the object and updates the corresponding values by multiplying them with 2. **Options Compared:** The test cases compare two different approaches to iterate over the key-value pairs of an object: 1. **For of**: This method uses a loop that is native to JavaScript, which may provide better performance since it doesn't require additional function calls or data structures. 2. **forEachKey**: This method uses a callback function with `Object.keys()` and `forEach()`, which may incur additional overhead due to the extra function call and data structure. **Pros and Cons:** Pros of each approach: * **For of**: Faster execution, as it's a native loop that doesn't require additional function calls or data structures. * **forEachKey**: More concise and expressive code, as it uses a familiar method for iterating over arrays. Cons of each approach: * **For of**: May not work as expected if the object is modified during iteration (e.g., by using `delete` to remove properties). * **forEachKey**: May be slower due to additional function calls and data structures. **Library:** The Lodash library is used in the benchmark definition. It provides utility functions for JavaScript, including `Object.fromEntries()`, which helps create objects from arrays of key-value pairs. **Special JS Feature/Syntax:** No special features or syntax are used in this benchmark. **Other Alternatives:** Other alternatives to iterate over an object's properties could include: 1. **`for...in` loop**: This method iterates over the object's property names (i.e., keys). It may not provide the same performance as `for of`, but it can be useful in certain situations. 2. **`Array.prototype.forEach()` with a custom iterator**: You could create a custom iterator using `Set` or other data structures to iterate over an object's properties. Keep in mind that these alternatives may have different trade-offs and requirements compared to the tested approaches. **Benchmark Result Interpretation:** The benchmark result shows the number of executions per second for each test case. A higher value indicates better performance. In this case, the `For of` loop appears to be faster than the `forEachKey` approach.
Related benchmarks:
native slice vs lodash slice
native slice vs lodash slice 1M
native-slice-vs-chunk
Unique lodash vs vanilla
_.merge vs _.assign vs JS reduce
Comments
Confirm delete:
Do you really want to delete benchmark?