Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects test
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.values
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 }, };
Tests:
for-in
for (var i=10000; i > 0; i--) { const array = [] for (var key in obj) { array.push({newid: obj[key].id}); } }
Object.keys
for (var i=10000; i > 0; i--) { const array = [] Object.keys(obj).forEach(key => array.push({newid: obj[key].id})); }
Object.values
for (var i=10000; i > 0; i--) { const array = [] Object.values(obj).forEach(n => array.push({newid: 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:
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):
**Benchmark Overview** The provided benchmark is designed to compare the performance of three different approaches for iterating over objects in JavaScript: `for-in`, `Object.keys()`, and `Object.values()`. **Options Compared** * `for-in`: This approach uses the `in` operator to iterate over the object's properties. It returns an array-like object containing all property names with their corresponding values. * `Object.keys()`: This approach uses a method of the `Object` prototype to return an array of a given object's own enumerable property names as strings. * `Object.values()`: This approach uses a method of the `Object` prototype to return an array containing the values of all properties with enumerable string keys or symbols. **Pros and Cons** * `for-in`: + Pros: Simple, straightforward, and widely supported. It allows iteration over both enumerable and non-enumerable properties. + Cons: Can be slower due to the overhead of iterating over all properties, including non-enumerable ones. * `Object.keys()`: + Pros: Efficient for most use cases, as it only iterates over enumerable properties. It's also a method on the `Object` prototype, making it easy to access and call. + Cons: May not be suitable for iterating over non-enumerable properties or symbols. * `Object.values()`: + Pros: Efficient for iterating over values of enumerable properties with string keys or symbols. It returns an array of values in the same order as they appear in the object. + Cons: Not available in older browsers (prior to Firefox 38) due to the use of `Symbol` objects. **Library Usage** None of the benchmark cases use external libraries, but `Object.keys()` and `Object.values()` do rely on methods that are part of the `Object` prototype. **Special JavaScript Features or Syntax** There are no special features or syntax used in this benchmark. However, it's worth noting that `for-in` can also iterate over non-enumerable properties if the object has a `__proto__` property (e.g., inherited properties). **Other Alternatives** If you wanted to compare other approaches for iterating over objects, some alternatives could include: * Using an array of property names and indexing into it * Using a `forEach()` loop with a custom callback function * Using a library like Lodash's `keyBy` or `valuesIn` * Using a library like jQuery's `.each()` However, these approaches are not included in the provided benchmark.
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?