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:
8 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'}; for (let i = 0; i < 1000; i++) _.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'}; for (let i = 0; i < 1000; i++) { 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'}; for (let i = 0; i < 1000; i++) { 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
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of three different approaches for iterating over an object's properties: `forIn`, `forOfEntries`, and `forInEntries`. The latter two are not standard JavaScript keywords, but rather a custom syntax introduced in ECMAScript 2019. **Options Compared** 1. **`forIn`**: This is the traditional way of iterating over an object's properties using the `in` operator. It returns an iterator that iterates over all property names (keys) of an object. 2. **`forOfEntries`**: This is a custom syntax introduced in ECMAScript 2019, which allows iterating over both key-value pairs and arrays. It uses the `of` keyword to specify the iterable. 3. **`forInEntries`**: Another custom syntax that seems to be an attempt to combine `forIn` and `forOfEntries`. However, it's not a standard JavaScript feature. **Pros and Cons** 1. **`forIn`**: * Pros: Wide support across browsers and Node.js versions. * Cons: Can be slower due to the need for explicit checks (`in` operator). 2. **`forOfEntries`**: * Pros: Improved performance, as it can take advantage of array iteration optimizations. * Cons: Not widely supported yet, especially in older browsers. 3. **`forInEntries`**: * Pros: None apparent, as it's not a standard JavaScript feature. * Cons: May be slower due to the complexity of its implementation. **Library Usage** The benchmark uses Lodash.js (version 4.17.5) for utility functions (`_.forIn`). This is likely used to simplify the code and provide a consistent way of iterating over objects. **Special JS Features or Syntax** * `of` keyword in `forOfEntries`: Introduced in ECMAScript 2019, this keyword allows specifying an iterable. * `_` prefix in Lodash functions: A common convention for using Lodash functions in a more concise manner. **Alternatives** Other alternatives for iterating over objects include: 1. Using `Object.keys()`, `Object.values()`, and `Object.entries()` to get the keys, values, or both as arrays. 2. Using `Array.prototype.forEach()` with an array of keys or values. 3. Using `Array.prototype.reduce()` to accumulate results. However, these alternatives may have different performance characteristics depending on the specific use case and browser/Node.js version. I hope this explanation helps! Let me know if you have any further questions.
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?