Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-of Object.entries vs for-in
(version: 0)
Comparing performance of:
native for-of w/ entries vs native for-in 2
Created:
6 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:
var obj = Array.from({ length: 10000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {})
Tests:
native for-of w/ entries
for (const [k, v] of Object.entries(obj)) {}
native for-in 2
for (const prop in obj) { const v = obj[prop]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native for-of w/ entries
native for-in 2
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to iterate over an object: `for-of` with `Object.entries` (native) and `for-in` (native). The goal is to determine which approach is faster. **Options Compared** 1. **Native `for-of` with `Object.entries`**: This method uses the `of` keyword to iterate over an array or an object, allowing for more concise code. In this case, it iterates over an object created by reducing a array of 10,000 elements. 2. **Native `for-in`**: This method uses the `in` operator to iterate over an object's properties. **Pros and Cons** 1. **Native `for-of` with `Object.entries`**: * Pros: More concise code, faster iteration (according to the benchmark). * Cons: May not work as expected if the object's structure changes. 2. **Native `for-in`**: * Pros: Works well with objects of any structure, but may require more code. * Cons: Can be slower than `for-of` with `Object.entries` (according to this benchmark). **Lodash Library** The benchmark includes a Lodash library reference in the HTML preparation code. Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object transformation, and more. In this specific case, the Lodash library is not used within the benchmark itself but may be useful if you were to extend or modify the benchmark code. **Special JavaScript Features or Syntax** There are no special JavaScript features or syntaxes mentioned in this benchmark. It only uses standard JavaScript keywords like `for`, `of`, and `in`. **Other Alternatives** If you want to test similar approaches, you could consider: 1. **Using a different iteration method**, such as `forEach` or `reduce`. 2. **Testing with different object sizes** or complexities. 3. **Comparing with other languages**, like Python or Java, using transpilers or compilers. Keep in mind that benchmark results may vary depending on the specific use case and environment. Always consider the specific requirements and constraints of your project when choosing an iteration approach.
Related benchmarks:
lodash vs for-of vs forEach
Loop over object: lodash vs Object.entries
Loop over object: lodash vs Object.entries 2
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries [2]
Comments
Confirm delete:
Do you really want to delete benchmark?