Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of against Object.entries vs for..in
(version: 0)
Comparing performance of:
for..of Object.entries() vs for..in vs for..of Object.entries() no destructuring
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({ length: 10000 }, () => { const n = Math.floor(Math.random() * 100) if (n > 26) { return n } else { return String.fromCharCode('a'.charCodeAt(0) + n); } }); var obj = {}; for (let i = 0; i < array.length; i++) { obj[i] = array[i]; }
Tests:
for..of Object.entries()
for (const [key, val] of Object.entries(obj)) { const foo = val + 'a'; }
for..in
for (const key in obj) { const val = obj[key]; const foo = val + 'a'; }
for..of Object.entries() no destructuring
for (const entry of Object.entries(obj)) { const key = entry[0]; const val = entry[1]; const foo = val + 'a'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for..of Object.entries()
for..in
for..of Object.entries() no destructuring
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 Explanation** The provided JSON represents a JavaScript microbenchmark test case, specifically comparing the performance of three different approaches: 1. `for...in` loop 2. Using `Object.entries()` and destructuring in a `for...of` loop 3. Using `Object.entries()` without destructuring in a `for...of` loop The benchmark is designed to measure the execution speed of these three approaches on a large array of objects, created using `Array.from()`. The array contains 10,000 elements, each with a random value between 0 and 100. For values greater than 26, a character string is generated using `String.fromCharCode()`. **Options Comparison** Here's a brief overview of the options being compared: * **`for...in` loop**: This approach iterates over the object's properties using the property name as the iteration variable. It's an older method that was commonly used before the introduction of `Object.entries()`. + Pros: Simple, widely supported. + Cons: May not be the fastest or most efficient way to iterate over an object's properties. * **`Object.entries()` and destructuring in a `for...of` loop**: This approach uses the `Object.entries()` method to get an array of key-value pairs from the object, which is then iterated over using a `for...of` loop with destructuring. + Pros: More efficient than `for...in`, as it avoids the overhead of property name lookups. Destructuring can also simplify the iteration logic. + Cons: May not be supported in older browsers or environments that don't have access to modern JavaScript features. * **`Object.entries()` without destructuring in a `for...of` loop**: This approach is similar to the previous one, but instead of using destructuring, it assigns the key and value variables directly from the iteration variable. + Pros: Similar efficiency benefits as before, with added flexibility for custom iteration logic. + Cons: Requires an extra assignment statement, which might incur a slight performance penalty. **Library Considerations** The benchmark uses `Object.entries()` to get an array of key-value pairs from the object. This method is part of the ECMAScript standard since ES6 (2015), so it's widely supported in modern browsers and environments. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark, other than the use of `String.fromCharCode()` for generating character strings, which is a built-in method. The benchmark focuses on comparing the performance of different iteration approaches, rather than leveraging advanced JavaScript features. **Alternative Approaches** Other alternatives to these three approaches might include: * Using `for...of` loop with an index variable instead of destructuring * Using `forEach()` or other array methods for iterating over the object's properties * Using a custom iterator function or a library like Lodash for iteration Keep in mind that these alternative approaches might not be as efficient or scalable as the original three options, especially for large datasets.
Related benchmarks:
Set.prototype.has() vs "in" operator
Set vs Objects vs Array
for..of against Map.entries vs for..in
for..of against Map.entries vs for..in 2
Comments
Confirm delete:
Do you really want to delete benchmark?