Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of
(version: 0)
Comparing performance of:
for-of vs for-in
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var DATA = {}; for (let i = 0; i < 1000; i++) { DATA["some-property-name-" + i] = "some-property-value-" + i; } function PROCESS(property, value) { }
Tests:
for-of
for (const [key, value] of Object.entries(DATA)) { PROCESS(key, value); }
for-in
for (const key in DATA) { const value = DATA[key]; PROCESS(key, value); }
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):
**What is being tested?** The benchmark on MeasureThat.net tests the performance difference between two approaches for iterating over an object in JavaScript: `for-in` and `for-of`. In the provided JSON, we have a script that prepares a dataset (`DATA`) with 1000 properties, each containing a string value. The purpose of this dataset is to serve as input for the benchmark. The test cases iterate over this dataset using two different approaches: 1. **for-in**: This approach uses the `in` operator to iterate over the object's property names. 2. **for-of**: This approach uses the `entries()` method on the `Object` prototype to iterate over an array of key-value pairs, which is then used with a `for...of` loop. **Options compared** The two options being compared are: * `for-in`: Iterates directly over property names using the `in` operator. * `for-of`: Iterates over key-value pairs as an array. **Pros and Cons of each approach:** * **for-in**: + Pros: - Simple and widely supported. - No need to create an array or handle iterations manually. + Cons: - May not be suitable for arrays or objects with complex structures, as it iterates over property names only. - Can lead to slower performance due to the overhead of checking each property name. * **for-of**: + Pros: - Suitable for arrays and objects with complex structures, as it iterates over key-value pairs. - Generally faster than `for-in` because it avoids the overhead of property name lookup. + Cons: - Requires support for the `entries()` method on the `Object` prototype (introduced in ECMAScript 2015). - May require additional work to handle edge cases, such as iterating over sparse arrays. **Library and special JS feature** There is no external library being used in this benchmark. However, the use of the `for-of` loop relies on a special JavaScript feature: the **iterable protocol**, which was introduced in ECMAScript 2015 (ES6). This protocol allows objects to be iterated over using methods like `entries()`. **Other alternatives** If you want to explore other approaches for iterating over an object, consider: * Using `Object.keys()`, `Object.values()`, or `Object.entries()` directly, without a loop. * Implementing a custom iterator using the `Symbol.iterator` property. * Using a library like Lodash or Ramda, which provide utility functions for working with objects. Keep in mind that these alternatives might introduce additional complexity and performance overhead compared to the `for-in` and `for-of` approaches.
Related benchmarks:
while vs for
Foreach vs for-in vs for-of vs for
let vs var for loop
For vs map vs forEach vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?