Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for in loop v2
(version: 0)
Comparing performance of:
mapForIn vs mapValues
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
mapForIn
const obj = { "1": { id: 1 }, "2": { id: 2 }, "3": { id: 3 }, "4": { id: 4 }, "5": { id: 5 }, "6": { id: 6 }, "7": { id: 7 }, "8": { id: 8 }, "9": { id: 9 }, "10": { id: 10 }, "11": { id: 11 }, "12": { id: 12 }, "13": { id: 13 }, "14": { id: 14 }, "15": { id: 15 }, "16": { id: 16 }, "17": { id: 17 }, "18": { id: 18 }, "19": { id: 19 }, "20": { id: 20 } }; const mapForIn = (func) => { const arr = []; for (let key in obj) { if (Object.hasOwnProperty(obj)) { arr.push(func(obj[key], arr.length - 1)); } } return arr; } mapForIn((item, index) => item);
mapValues
const obj = { "1": { id: 1 }, "2": { id: 2 }, "3": { id: 3 }, "4": { id: 4 }, "5": { id: 5 }, "6": { id: 6 }, "7": { id: 7 }, "8": { id: 8 }, "9": { id: 9 }, "10": { id: 10 }, "11": { id: 11 }, "12": { id: 12 }, "13": { id: 13 }, "14": { id: 14 }, "15": { id: 15 }, "16": { id: 16 }, "17": { id: 17 }, "18": { id: 18 }, "19": { id: 19 }, "20": { id: 20 } }; const mapValues = (func) => Object.values(obj).map(func); mapValues((item, index) => item);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
mapForIn
mapValues
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 dive into the explanation of the provided benchmark. **Benchmark Overview** The provided benchmark compares two approaches to iterate over an object: `Object.values` and a custom implementation using a `for...in` loop (`mapForIn`) or array methods (`mapValues`). The goal is to determine which approach is faster on JavaScript engines, specifically on Chrome 70 running on Linux. **Options Compared** 1. **Object.values**: This method returns an array of the object's values. 2. **mapForIn**: A custom implementation using a `for...in` loop that iterates over the object's keys and calls a provided function for each value. 3. **mapValues**: Another custom implementation using `Object.values` to get an array of values, which is then mapped over with a provided function. **Pros and Cons** 1. **Object.values**: * Pros: Simple and efficient way to get object values as an array. * Cons: May not be the fastest approach since it requires creating an intermediate array. 2. **mapForIn**: * Pros: Can provide more control over iteration, especially when working with complex objects or custom data structures. * Cons: May be slower due to the use of a `for...in` loop and additional function calls. 3. **mapValues**: * Pros: Combines the efficiency of `Object.values` with the flexibility of array methods like `map`. * Cons: Requires two function calls, which may add overhead. **Library Used** In this benchmark, `Array.prototype.map()` is used in both `mapForIn` and `mapValues`. This library provides a convenient way to perform operations on arrays, but it also incurs some overhead due to the creation of intermediate objects. **Special JS Feature/Syntax** The `for...in` loop in `mapForIn` uses the `Object.hasOwnProperty()` method to check if the current iteration is a property of the object. This is not a special feature per se, but rather an optimization used by some JavaScript engines to avoid iterating over inherited properties. **Other Alternatives** If you're looking for alternative approaches to iterate over objects in JavaScript, consider the following: * **for...of**: A newer `for` loop syntax that can be used to iterate over arrays and objects using a simple loop. * **Array.prototype.forEach()`: Similar to `map()`, but without the return value. * **Object.keys()`**: Returns an array of object keys, which can then be iterated over using a loop. Keep in mind that these alternatives may have different trade-offs in terms of performance, control, and code readability.
Related benchmarks:
For in vs For of
For in vs Object.*.forEach
For in vs Object.*.forEach vs Object.values :D
Object entries vs forin
for-in vs object.keys vs object.values for objects perf
Comments
Confirm delete:
Do you really want to delete benchmark?