Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs Object.keys benchmark 3
(version: 2)
Comparing performance of:
for-in vs Object.keys with forEach vs Object.keys with reduce vs Object.keys with for-of vs for-in with hasOwnProperty
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var codes = Array.from(Array(26)).map((_, i) => i + 65); var alphabet = codes.reduce((acc, code) => { acc[String.fromCharCode(code)] = code; return acc; }, {});
Tests:
for-in
for (var i=1000; i > 0; i--) { var result = {}; for (var key in alphabet) { result[key] = alphabet[key]; } };
Object.keys with forEach
for (var i=1000; i > 0; i--) { var result = {}; Object.keys(alphabet).forEach(key => { result[key] = alphabet[key]; }); };
Object.keys with reduce
for (var i=1000; i > 0; i--) { var result = Object.keys(alphabet).reduce((acc, key) => { acc[key] = alphabet[key]; return acc; }, {}); };
Object.keys with for-of
for (var i=1000; i > 0; i--) { var result = {}; for (var key of Object.keys(alphabet)) { result[key] = alphabet[key]; } };
for-in with hasOwnProperty
for (var i=1000; i > 0; i--) { var result = {}; for (var key in alphabet) { if (Object.prototype.hasOwnProperty.call(alphabet, key)) { result[key] = alphabet[key]; } } };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for-in
Object.keys with forEach
Object.keys with reduce
Object.keys with for-of
for-in with hasOwnProperty
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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The test compares four different approaches to iterate over an object using `Object.keys()`: 1. **For-in**: Iterates directly over the object's own enumerable properties. 2. **Object.keys() with forEach**: Uses `Object.keys()` to get an array of the object's keys, and then iterates over that array using `forEach`. 3. **Object.keys() with reduce**: Uses `Object.keys()` to get an array of the object's keys, and then iterates over that array using `reduce` to build an object. 4. **For-in with hasOwnProperty**: A variation of the traditional for-in loop that checks if each key is present in the original object before assigning it. **Pros and Cons of Each Approach** 1. **For-in**: * Pros: Can access properties without calling `hasOwnProperty()` on the object, which can be faster. * Cons: May iterate over non-enumerable properties (e.g., prototype chain), and some browsers may not support this method. 2. **Object.keys() with forEach**: * Pros: Safe and consistent across all browsers, as it uses `forEach` to iterate over the array returned by `Object.keys()`. * Cons: Requires an additional function call to convert the result of `Object.keys()` to an array, which may introduce performance overhead. 3. **Object.keys() with reduce**: * Pros: Similar to using `forEach`, but allows for more flexibility in building the resulting object. * Cons: May be slower than the other two approaches due to the additional function call and potential overhead of reducing an array. 4. **For-in with hasOwnProperty**: * Pros: Combines the benefits of traditional for-in loops (fastest) with an added layer of safety to prevent errors. * Cons: Requires checking each key, which can add performance overhead. **Library and Purpose** The `Object.keys()` method is a built-in function in JavaScript that returns an array of the object's own enumerable properties. This benchmark uses `Object.keys()` as a common interface for iterating over objects across different browsers. **Special JS Features or Syntax** None mentioned in the provided code. However, it's worth noting that this benchmark does not test any modern JavaScript features like ES6 classes, modules, or async/await, which may have an impact on performance due to syntax parsing and execution overhead. **Other Alternatives** If you're interested in exploring alternative approaches to iterating over objects, here are a few: * **Using `for...in` with a custom loop**: Instead of using `Object.keys()` as the array source, you can write your own loop to iterate over the object's properties. * **Using a library like Lodash or Ramda**: These libraries provide optimized functions for working with arrays and objects, which may offer performance benefits in certain cases. Keep in mind that the choice of iteration method ultimately depends on your specific use case and the trade-offs you're willing to make between performance, safety, and readability.
Related benchmarks:
Object.fromEntries vs reduce vs Map vs foreach
for-in vs object.keys vs object.values for objects perf 5
Object.fromEntries vs reduce vs Map vs for of vs for
Object.fromEntries vs reduce object.assign vs for in
Object.fromEntries vs reduce vs Map vs for of
Comments
Confirm delete:
Do you really want to delete benchmark?