Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.values
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': { id: 'a', num: 1 }, 'b': { id: 'b', num: 1 }, 'c': { id: 'c', num: 1 }, 'd': { id: 'd', num: 1 }, 'e': { id: 'e', num: 1 }, 'f': { id: 'f', num: 1 }, 'g': { id: 'g', num: 1 }, };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { console.log(obj[key].id); } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(obj[key].id)); }
Object.values
for (var i=10000; i > 0; i--) { 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:
28 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
2.9 Ops/sec
Object.keys
2.8 Ops/sec
Object.values
2.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested:** The provided benchmark tests the performance of three different approaches to iterate over objects in JavaScript: 1. `for-in` loop 2. `Object.keys()` method 3. `Object.values()` method These approaches are used to print the `id` property of each object in the input JSON object. **Options compared:** * `for-in`: uses a traditional for loop with an explicit iteration variable (`var i = 10000;`) and checks each property of the object using `in`. * `Object.keys()`: uses the `Object.keys()` method to get an array of property names of the object, which is then iterated over using `forEach()`. * `Object.values()`: uses the `Object.values()` method to get an array of property values of the object, which is then iterated over using `forEach()`. **Pros and Cons:** * `for-in` loop: + Pros: simple and widely supported. + Cons: can be slow due to the overhead of checking each property in the object. + Can also iterate over inherited properties, which may not be desirable. * `Object.keys()`: + Pros: modern and efficient way to get an array of property names. + Cons: requires the presence of a `for...of` loop or `forEach()` method, which can add overhead. * `Object.values()`: + Pros: provides a direct way to get an array of values without iterating over property names. + Cons: may not be supported in older browsers or environments. **Library usage:** None of the benchmark cases use any external libraries. The `Object.keys()` and `Object.values()` methods are built-in JavaScript features. **Special JS feature or syntax:** No special JavaScript features or syntax are used in these benchmark cases. However, it's worth noting that the `for...of` loop (not explicitly used here) is a relatively new feature introduced in ECMAScript 2015, and its usage may affect performance. **Other alternatives:** Other alternatives to iterate over objects could include: * Using `Array.prototype.forEach()` with an array of property names or values. * Using a custom function to iterate over the object's properties. * Using a library like Lodash or Ramda for functional programming approaches. Keep in mind that these alternatives may have different performance characteristics and may not be supported in all browsers or environments.
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
for... in VS Object.keys() VS Object.entries()
Object.keys vs Object.entries vs Object.values
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?