Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for in loop
(version: 0)
Comparing performance of:
mapForIn vs mapValues
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const obj = {}; for (let i = 0; i >= 100; i++) { obj[i] = { id: i }; }; console.log(obj); const mapForIn = (func) => { const arr = []; for (let key in obj) { if (Object.hasOwnProperty(obj)) { arr.push(func(obj[key], arr.length - 1)); } } return arr; } const mapValues = (func) => Object.values(obj).map(func);
Tests:
mapForIn
const obj = {}; for (let i = 0; i >= 100; i++) { obj[i] = { id: i }; }; console.log(obj); 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 = {}; for (let i = 0; i >= 100; i++) { obj[i] = { id: i }; }; console.log(obj); 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 127 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
mapForIn
191921.4 Ops/sec
mapValues
186700.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of two approaches for iterating over an object: `for...in` loop and `Object.values()` method. **Options Compared** 1. **For...in Loop**: This is a traditional way to iterate over an object's properties in JavaScript. It uses the `for...in` keyword, which iterates over both own enumerable and non-enumerable properties of the object. 2. **Object.values() Method**: This method returns an array containing all values of an object's own enumerable properties. **Pros and Cons** * **For...in Loop**: + Pros: Allows iteration over both own enumerable and non-enumerable properties, can be more flexible when dealing with complex objects. + Cons: May have performance issues due to the overhead of checking for non-enumerable properties, may not work correctly in older browsers. * **Object.values() Method**: + Pros: Fast and efficient, works well on modern browsers, returns only enumerable values. + Cons: Limited control over iteration order (all own enumerable properties), does not support iteration over non-enumerable properties. **Library and Special JS Feature** The `mapForIn` function uses the `Object.hasOwnProperty()` method to ensure that it only pushes elements from the `obj` object to the array. This is necessary because the `for...in` loop also iterates over non-enumerable properties, which would not be included in the array. **Benchmark Preparation Code** The script preparation code creates an object `obj` with 100 properties, each containing an `id` property set to a unique number from 0 to 99. The `mapForIn` and `mapValues` functions are defined as specified in the benchmark definition. **Individual Test Cases** There are two individual test cases: 1. **mapForIn**: This test case uses the `for...in` loop approach, with the function `(item, index) => item` applied to each property of the object. 2. **mapValues**: This test case uses the `Object.values()` method approach, with the same function `(item, index) => item` applied to each enumerable value of the object. **Latest Benchmark Result** The benchmark result shows that the Chrome Mobile 127 browser executes the `mapForIn` loop approximately 5.2% faster than the `mapValues` method, with an execution rate of 191,921.375 executions per second for `mapForIn` and 186,700.671875 executions per second for `mapValues`. **Other Alternatives** If you want to use a different approach, you can consider: * Using `Object.entries()` method to iterate over both enumerable and non-enumerable properties. * Using `for...of` loop instead of `for...in` loop. * Using a library like Lodash or Ramda that provides optimized iteration functions.
Related benchmarks:
Object.values vs for in loop vs for loop
Object.values vs for in loop vs for loop v2
Object.values vs for in loop vs for loop v1 borys
Object.values vs for in loop vs for loop v2 borys
Comments
Confirm delete:
Do you really want to delete benchmark?