Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of-entires
(version: 0)
Comparing performance of:
for of vs for in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, 0: 'a', 1: 'b', str: 'long string', nested: { nested: 0 }, hello: 55.5 }; var copy = {}
Tests:
for of
for(let i=0;i<10000;i++){ let itrator = Object.entries(obj) for(let [k,v] of itrator){ copy[k] = v } }
for in
for(let i=0;i<10000;i++){ for(let k in obj){ copy[k] = obj[k] } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of
for in
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 provided benchmark JSON and explain what's being tested, compared, and other considerations. **Benchmark Definition** The benchmark is defined by two scripts: `Script Preparation Code` and `Html Preparation Code`. The script preparation code defines an object `obj` with various properties, including some numerical values and a nested object. A copy object `copy` is also created as part of the benchmark setup. **Test Cases** There are two test cases: 1. **for-of-entires**: This test case uses the `Object.entries()` method to get an array-like object containing the key-value pairs of the `obj` object. The code then iterates over this array using a `for...of` loop and assigns each value to a property in the `copy` object. 2. **for-in**: This test case uses the `for...in` loop to iterate over the properties of the `obj` object, assigning each value to a property in the `copy` object. **Comparison** The comparison being tested is between the performance of using `Object.entries()` with a `for...of` loop versus using `for...in`. This is a common topic of debate among JavaScript developers, and MeasureThat.net provides a convenient way to compare these two approaches. **Pros and Cons:** ### for-of-entires (with Object.entries()) Pros: * More modern and concise syntax * Efficient iteration over key-value pairs * No need to check for object properties using `hasOwnProperty()` Cons: * Requires JavaScript engine support for `Object.entries()` (introduced in ES6) * May not be compatible with older browsers or engines ### for-in Pros: * Wide compatibility across browsers and engines * Easy to implement and understand * Works on non-object values as well (e.g., arrays, numbers) Cons: * Less efficient iteration over key-value pairs compared to `Object.entries()` * Requires additional code to check for object properties using `hasOwnProperty()` to avoid property enumeration in prototype chains **Library** None of the test cases use any external libraries. **Special JS Features or Syntax** The benchmark uses the following special JavaScript features: * ES6 syntax: `for...of` loop, `Object.entries()` * ES6-style object literals * Template literals (`\r\n`) Note that these features are widely supported in modern browsers and engines, but may not be compatible with older systems. **Alternatives** Other alternatives for iterating over an object's properties include: * Using a traditional `for` loop with indexing (`for (var i = 0; i < obj.length; i++) { ... }`) * Using the `forEach()` method on the object itself (`obj.forEach(function(value, key) { ... })`) * Using the `map()` method to create an array of values or keys (`Object.keys(obj).map(key => obj[key])`) However, these alternatives may not be as efficient or readable as using `for...of` with `Object.entries()`.
Related benchmarks:
For in vs For of
dxcsxfr
Object entries vs forin
in vs not undefined
de vs des
Comments
Confirm delete:
Do you really want to delete benchmark?