Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forIn vs forOfEntries
(version: 0)
Comparing performance of:
forIn vs forOfEntries vs forInEntries
Created:
7 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>
Tests:
forIn
const obj = {a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g', h: 'h', i: 'i', j: 'j'}; _.forIn(obj, (val, key) => { console.log(val, key); });
forOfEntries
const obj = {a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g', h: 'h', i: 'i', j: 'j'}; const entries = Object.entries(obj); for ([key, val] of entries) { console.log(val, key); }
forInEntries
const obj = {a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g', h: 'h', i: 'i', j: 'j'}; const entries = Object.entries(obj); for ([key, val] in entries) { console.log(val, key); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forIn
forOfEntries
forInEntries
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
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat compares the performance of three different approaches to iterating over the properties of an object in JavaScript: **1. `_.forIn`:** This method comes from the Lodash library ([https://lodash.com/](https://lodash.com/)). Lodash is a popular library that provides utility functions for working with JavaScript objects, arrays, and strings. * **Pros:** Concise syntax, handles properties in arbitrary order. * **Cons:** Can be slightly slower than other methods due to the overhead of using a third-party library. **2. `for...of` with `Object.entries`:** This approach uses the modern `for...of` loop to iterate over an array of key-value pairs obtained from `Object.entries(obj)`. * **Pros:** Modern syntax, generally considered efficient and widely supported. * **Cons:** Requires explicit conversion of the object to an array using `Object.entries`. **3. `for...in` with `Object.entries`:** This approach uses the traditional `for...in` loop to iterate over an array of key-value pairs obtained from `Object.entries(obj)`. * **Pros:** Familiar syntax for developers used to older JavaScript versions. * **Cons:** Can be less efficient than `for...of`, especially in modern JavaScript engines. **Other Considerations:** * **Iteration Order:** In most cases, the order of iteration is not critical. * **Optimization:** Modern JavaScript engines often optimize iterations over objects, so performance differences might be subtle in real-world scenarios. **Alternatives:** * **Map:** If you need to maintain a specific insertion order or perform frequent updates, using a Map object instead of an object could be more efficient and provide better control. * **Async/Await:** For asynchronous operations involving objects, consider using async/await for improved readability and error handling. Ultimately, the best approach depends on your specific use case, coding style preferences, and project requirements.
Related benchmarks:
benchmark--------7
isEmpty vs. vanilla
Native array length vs Lodash's isEmpty
Lodash vs plain
Lodash some vs isEmpty 2
Comments
Confirm delete:
Do you really want to delete benchmark?