Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs for..of object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys vs for-of object.keys
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 (var key in obj) { console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(key)); }
for-of object.keys
for (var i=10000; i > 0; i--) { for (const key of Object.keys(obj)) { console.log(key) } }
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-of object.keys
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. **Benchmark Definition** The benchmark is designed to compare three different ways of iterating over an object in JavaScript: 1. `for-in` 2. `Object.keys` (with `forEach`) 3. `for...of` with `Object.keys` **Options being compared** * **`for-in`**: This method iterates over the object's properties using the property names as strings. * **`Object.keys` + `forEach`**: This method uses the `Object.keys()` function to get an array of property names, and then loops through it using `forEach`. * **`for...of` with `Object.keys`**: This is a more modern way of iterating over arrays and objects. It uses a for-of loop that iterates over the values returned by `Object.keys()`. **Pros and Cons** * **`for-in`**: + Pros: Simple, easy to understand. + Cons: Can be slower due to the overhead of property iteration. + Caveat: The `in` operator also returns inherited properties, which might not be what you want. * **`Object.keys` + `forEach`**: + Pros: More explicit and easier to understand than `for-in`. + Cons: Can be slower due to the overhead of array iteration. Also, it doesn't work well with objects that have a large number of inherited properties. * **`for...of` with `Object.keys`**: + Pros: Modern, efficient, and easy to read. + Cons: Requires modern JavaScript support (ECMAScript 2015 and later). **Library/Functionality** None. This benchmark uses built-in JavaScript functions and syntax. **Special JS Feature/Syntax** The benchmark uses the `for...of` loop with array iteration, which is a relatively new feature in JavaScript. It also uses the `Object.keys()` function to get an array of property names. **Other Alternatives** For iterating over objects in JavaScript, other options include: * `Array.prototype.forEach.call()`: Similar to `forEach`, but requires calling it on an array-like object. * `for...in` with a check for `undefined`: To avoid iterating over inherited properties. * `Object.values()` (ES2019+): Returns an array of property values, not names. In summary, the benchmark compares three different ways of iterating over an object in JavaScript: `for-in`, `Object.keys + forEach`, and `for...of` with `Object.keys`. It highlights the pros and cons of each approach and provides a simple test case to demonstrate their performance differences.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
Object.entries vs Object.keys vs for...in
for-in vs for object.keys keys
Comments
Confirm delete:
Do you really want to delete benchmark?