Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects perf 5
(version: 1)
Comparing performance of:
for-in vs Object.keys vs Object.values
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj =[...new Array(10000)].reduce((o,_,i)=>{ o[i+'-id']={id:i}; return o; },{});
Tests:
for-in
for (var key in obj) { console.log(obj[key].id); }
Object.keys
Object.keys(obj).forEach(key => console.log(obj[key].id));
Object.values
Object.values(obj).forEach(n => console.log(n.id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.713 YaBrowser/23.9.5.713 (corp) Yowser/2.5 Safari/537.36
Browser/OS:
Yandex Browser 23 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
31.1 Ops/sec
Object.keys
34.3 Ops/sec
Object.values
34.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark, "for-in vs object.keys vs object.values for objects perf 5", compares the performance of three different approaches to iterate over an array of objects in JavaScript: 1. `for-in` 2. `Object.keys()` 3. `Object.values()` (introduced in ECMAScript 2019) **Options Compared** The benchmark tests the execution speed of each approach on a large dataset of 10,000 objects. * `for-in`: uses a traditional for loop to iterate over the object's properties * `Object.keys()`: returns an array of property names that can be iterated over using a foreach loop or other iteration methods * `Object.values()`: returns an array of property values that can also be iterated over **Pros and Cons** Here are some pros and cons of each approach: ### for-in Pros: * Simple to implement and understand * Works with both objects and arrays Cons: * Can iterate over inherited properties, which may not be desirable in all cases * May have performance issues if the object has a large number of properties ### Object.keys() Pros: * Returns an array of property names only (not including inherited or computed properties) * Can be more efficient than `for-in` because it avoids iterating over unnecessary properties Cons: * Requires additional code to iterate over the returned array * May have performance issues if the object is very large ### Object.values() Pros: * Returns an array of property values only (not including keys or computed properties) * Can be more efficient than `Object.keys()` because it avoids iterating over unnecessary arrays * Introduced in ECMAScript 2019, making it a modern and optimized choice Cons: * May have performance issues if the object is very large due to the overhead of creating an array * Requires additional code to iterate over the returned array **Library Usage** None. **Special JS Features or Syntax** None mentioned. **Alternative Approaches** Other approaches to iterate over objects in JavaScript include: 1. Using `Array.prototype.forEach()` with a callback function 2. Using `for...of` loops (introduced in ECMAScript 2015) 3. Using libraries like Lodash's `_.forEachObject()` or underscore.js's `_each()` However, the benchmark is primarily focused on comparing the performance of `Object.keys()` and `Object.values()` with traditional for loops (`for-in`) and foreach loops (`Array.prototype.forEach()`), making these alternatives less relevant to this specific comparison.
Related benchmarks:
Object.entries + reduce VS Object.keys + forEach (fixed)
Object.fromEntries vs reduce v21
Object.fromEntries vs reduce round 2
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?