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'}; _.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
llama3.1:latest
, generated one year ago):
Let's break down what's being tested here. **Benchmark Name and Description** The benchmark is called "forIn vs forOfEntries". It compares the performance of two different ways to iterate over an object's properties in JavaScript: `for...in` loop versus using the `Object.entries()` method with a `for...of` loop. **Test Cases** There are three test cases: 1. **`forIn`**: This test uses a traditional `for...in` loop to iterate over an object's properties, along with the Lodash library's `_.forIn()` function to achieve similar behavior. 2. **`forOfEntries`**: This test uses a modern `for...of` loop to iterate over the result of calling `Object.entries()`, which returns an array of key-value pairs from the object. 3. **`forInEntries`**: This test is similar to `forOfEntries`, but it uses a traditional `for...in` loop instead of `for...of`. However, it incorrectly attempts to use the same syntax as `for...of`, which will not work in this context (more on that later). **Latest Benchmark Result** The latest benchmark result shows the execution speed (Executions Per Second) for each test case: * `forOfEntries` runs at 3517.693359375 executions per second. * `forInEntries` runs at 3360.0 executions per second. * `forIn` runs at 3314.451171875 executions per second. **Library Used** The Lodash library is used in the test case for `forIn`. It's a popular utility library that provides various functional programming helpers, including a `_.forIn()` function that iterates over an object's properties and performs a callback function for each property. **Special JS Feature or Syntax** There are two special features being tested here: * **`Object.entries()`**: This is a modern feature introduced in ECMAScript 2017 (ES8) that returns an array of key-value pairs from an object. * **`for...of` loop**: This is a modern `for` loop syntax introduced in ES6, which allows iterating over arrays or iterable objects. **Pros and Cons** Here are some general pros and cons for each approach: * `for...in` loop: + Pros: simple to use, works with older JavaScript engines. + Cons: may iterate over inherited properties, can be slow if the object has many properties. * Using `Object.entries()` with `for...of` loop: + Pros: efficient, easy to read, and write, doesn't iterate over inherited properties. + Cons: requires modern JavaScript engine support (ES8+). **Other Alternatives** There are other ways to iterate over an object's properties in JavaScript: * **`Object.keys()`**: Returns an array of keys from the object. * **`Object.getOwnPropertyNames()`**: Returns an array of all property names, including non-enumerable ones. Keep in mind that these alternatives might not be as efficient or convenient as using `for...of` loops with `Object.entries()`.
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?