Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects vs arr
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.values vs Array for vs Array forEach
Created:
2 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 }, }; var arr = [ { id: 'a', num: 1 }, { id: 'b', num: 1 }, { id: 'c', num: 1 }, { id: 'd', num: 1 }, { id: 'e', num: 1 }, { id: 'f', num: 1 }, { 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)); }
Array for
for (var i=10000; i > 0; i--) { for (var j=0,l=arr.length; j < l; j++) { console.log(arr[j].id); } }
Array forEach
for (var i=10000; i > 0; i--) { arr.forEach(n => console.log(n.id)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.values
Array for
Array forEach
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of different ways to iterate over objects and arrays in JavaScript. **Options Compared:** * **`for...in` (Object Iteration):** This traditional loop iterates through the *keys* of an object. It's simple but can be slower than other methods due to its reliance on enumerating properties, which might include inherited ones. * **`Object.keys()` (Array of Keys):** This method returns an array of the object's own keys. It's generally faster than `for...in` because it avoids iterating through unnecessary properties. * **`Object.values()` (Array of Values):** Returns an array containing the *values* of an object's own properties. Useful if you need direct access to values instead of just keys. * **`for...loop` with Array:** This standard loop iterates over each element in an array directly by index, which is usually the most efficient way to work with arrays. * **`forEach()` (Array Iteration):** A higher-order function that executes a provided callback function for each element in an array. It offers a more concise syntax than `for...loop`. **Pros and Cons:** | Method | Pros | Cons | |--------|------------------------------------------|-----------------------------------| | `for...in`| Simple, easy to understand | Can be slower due to property enumeration | | `Object.keys()`| Faster than `for...in`, returns keys as array | Doesn't return values | | `Object.values()` | Returns values directly | Doesn't return keys | | `for...loop` (Array) | Generally the fastest for arrays | More verbose | | `forEach()` | Concise syntax | Less direct control over iteration | **Alternatives:** While not shown in this benchmark, other methods like `map()`, `filter()`, and `reduce()` provide powerful ways to transform or analyze array data. **Key Takeaways:** * For arrays, using a `for...loop` or `forEach()` is generally the most efficient way to iterate. * `Object.keys()` and `Object.values()` are good choices when working with object keys and values respectively. `for...in` should be used with caution due to its potential performance issues. Let me know if you have any other questions!
Related benchmarks:
for-in vs object.keys vs object.values for objects v2
for-in vs object.keys vs object.values for objects - output object values
for-in vs object.keys vs object.values for objects 2.0
Yet Another Object.keys vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?