Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys (forEach) vs object.keys (for)
(version: 0)
Comparing performance of:
for-in vs Object.keys for each vs Object.keys for
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (const key in obj) { // Do nothing } }
Object.keys for each
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach((key) => { // Do nothing }); }
Object.keys for
for (var i=10000; i > 0; i--) { const keys = Object.keys(obj) for (let a = 0; a < keys.length; a++) { // Do nothing } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys for each
Object.keys for
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):
I'll break down the benchmark and explain what's being tested, compared, and discussed. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches: 1. `for-in` loop 2. Using `Object.keys()` with `forEach()` 3. Using `Object.keys()` without `forEach()` ( manual iteration) **Options Compared** * **For-in loop**: This traditional way of iterating over an object's properties uses the `in` operator to check if a property is present in the object. * **Object.keys()` with `forEach()`**: This approach uses the `Object.keys()` method to get an array of the object's property names, and then iterates over that array using `forEach()`. * **Object.keys()` without `forEach()` ( manual iteration ): This option skips using `forEach()` and instead manually iterates over the array returned by `Object.keys()`. **Pros and Cons** 1. **For-in loop**: * Pros: Simple, easy to understand, and often faster since it doesn't involve an additional function call. * Cons: May not be as efficient for large objects since it uses a separate iterator object. 2. **Object.keys()` with `forEach()`**: * Pros: More concise and expressive code compared to the traditional `for-in` loop. * Cons: May introduce overhead due to the additional function call, which can be slower than native iteration. 3. **Object.keys()` without `forEach()` ( manual iteration )**: * Pros: No additional function call overhead, making it potentially faster for large objects. * Cons: More verbose and error-prone code. **Library Used** The benchmark uses the built-in JavaScript `Object` and `Array` libraries. Specifically: * `Object.keys()`: Returns an array of a given object's property names as strings. * `forEach()`: A method that calls a provided function once for each element in an array. **Special JS Feature/Syntax** None mentioned explicitly, but the benchmark uses JavaScript syntax and features such as: * ES6-style template literals (`\r\n` formatting) * Function expression syntax (e.g., `(key) => {\t// Do nothing\r\n}`) * Array iteration syntax (e.g., `for (let a = 0; a < keys.length; a++) { ... }`) **Alternatives** If you want to compare the performance of other iteration methods, consider using: 1. **Native array iteration**: Using the traditional `for` loop with indexing (`arr[i]`) instead of `forEach()`. 2. **Array.prototype.reduce()`: Using the `reduce()` method to iterate over an array and accumulate a result. 3. **Array.prototype.some() / Array.prototype.every()**: Using the `some()` or `every()` methods to check if any/ all elements in an array satisfy a condition. Keep in mind that these alternatives may have different performance characteristics depending on the specific use case and JavaScript engine used.
Related benchmarks:
For in vs For of
for-in vs Object.keys()
Object.entries vs Object.keys vs for...in
for-in vs object keys map vs object keys loop
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?