Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of-entires with class
(version: 0)
Comparing performance of:
for of vs for in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class A { a = 1 b = 2 c = 3 d = 4 method1(){} method2(){} } class B extends A { str = 'long string' nested = { nested: 0 } method3(){} method4(){} } class C extends B { hello = 55.5 method4(){} method5(){} } var obj = new C() var copy = {}
Tests:
for of
for(let i=0;i<1000;i++){ let itrator = Object.entries(obj) for(let [k,v] of itrator){ copy[k] = v } }
for in
for(let i=0;i<1000;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 and explain what is being tested. **Benchmark Overview** The benchmark compares the performance of two `for` loops: one using `Object.entries()` with `for...of`, and another using `for...in`. The test case uses classes `A`, `B`, and `C` to create an object hierarchy, where each class extends the previous one. **Options Being Compared** The benchmark is comparing the performance of two approaches: 1. **`Object.entries()` with `for...of`**: This approach uses the `Object.entries()` method to get an array of key-value pairs from the object, and then iterates over this array using a `for...of` loop. 2. **`for...in`**: This approach uses the `for...in` loop to iterate directly over the object's properties. **Pros and Cons of Each Approach** 1. **`Object.entries()` with `for...of`**: * Pros: More concise and expressive, as it avoids explicit property iteration. * Cons: May incur additional overhead due to method call and array creation. 2. **`for...in`**: * Pros: May be faster, as it avoids the overhead of method calls and array creation. * Cons: Less concise and less expressive, as it requires manual property iteration. **Library Used** In this benchmark, the `Object.entries()` method is used, which is a part of the JavaScript standard library. The purpose of this method is to return an array of key-value pairs for each object in the passed iterable or mapped object. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax beyond the standard `for` loops and `Object.entries()` method. **Benchmark Preparation Code** The preparation code creates three classes: `A`, `B`, and `C`. The class hierarchy is defined such that each class extends the previous one, creating a nested object structure. An instance of the top-level class `C` is created, which serves as the source data for the benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, consider using: 1. **`Array.prototype.forEach()`**: This method can be used to iterate over an array of key-value pairs returned by `Object.entries()`. 2. **`for...of` with manual iteration**: You could use a manual loop to iterate over the object's properties using the `in` operator, similar to the `for...in` approach. Keep in mind that these alternatives may not offer significant performance benefits and are primarily used for educational or illustrative purposes. For most use cases, the choice between `Object.entries()` with `for...of` and `for...in` depends on personal preference and code readability. If conciseness and expressiveness are important, `Object.entries()` with `for...of` may be a better choice. If raw performance is critical, or if you need to support older browsers that don't support `Object.entries()`, `for...in` might be a more viable option.
Related benchmarks:
Closure plus class check versus function apply
Class comp
member vs proto member
Wrapping objects method removal performance
Comments
Confirm delete:
Do you really want to delete benchmark?