Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of Object.values vs for in b
(version: 0)
Comparing performance of:
for of Object.values vs for in
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = {} for (let i=0; i< 100000; i++) { a[[...Array(5)].map(i=>(~~(Math.random()*36)).toString(36)).join('')] = Math.random()*36; }
Tests:
for of Object.values
for (let row of Object.values(a)) { row += 1; }
for in
for (const key in a) { let row = a[key]; row += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of Object.values
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's being tested, compared, and the pros and cons of different approaches. **Benchmark Overview** The benchmark compares two ways to iterate over an object in JavaScript: 1. `for...of` with `Object.values()` (Test Case 1) 2. `for...in` (Test Case 2) **Library Used: None** Neither of the test cases uses a specific library. The focus is solely on comparing the performance of two built-in JavaScript iteration methods. **Special JS Feature/Syntax:** * `Array.prototype.map()`: used to generate an array of random base-36 strings. * `Object.values()`: a method introduced in ECMAScript 2017 that returns an array containing all own enumerable property values of an object. * `for...of` and `for...in` loops: built-in loop constructs. **Test Case 1: `for...of` with `Object.values()`** This test case uses `for...of` to iterate over the values of an object, which is created using `Object.values()`. The loop increments each value by 1. Pros: * Easy to read and understand. * Native JavaScript support, so no additional library required. Cons: * May incur a higher overhead due to the creation of an intermediate array (`Object.values()`). * Limited control over iteration order (values are iterated in the order they appear in the object). **Test Case 2: `for...in`** This test case uses traditional `for...in` loops to iterate over the properties of an object, accessing each property value using the bracket notation (`a[key]`). The loop increments each value by 1. Pros: * More control over iteration order (properties are iterated in the order they appear in the object). * May be more efficient for large objects since it doesn't create an intermediate array. Cons: * Less readable and maintainable due to the use of bracket notation. * May incur higher overhead due to string manipulation. **Other Alternatives** If you wanted to test alternative iteration methods, you could consider using: * `forEach()`: a method that executes a function for each element in an array (not applicable here since we're iterating over objects). * `entries()` or `keys()`: methods that return iterators for object entries and keys, respectively. * Custom iteration logic using `Object.keys()`, `Object.entries()`, or other methods. Keep in mind that the performance differences between these methods may not be significant unless you're working with extremely large datasets. The benchmark provided by MeasureThat.net likely aims to highlight any potential performance disparities between these two popular iteration methods.
Related benchmarks:
keys vs values
for of Object.values vs for in a
Map vs Object with Number Keys
Map -> Array
Comments
Confirm delete:
Do you really want to delete benchmark?