Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries vs for..in
(version: 0)
Comparing performance of:
Object.entries (forEach) vs for..in vs Object.entries (for loop)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, 0: 'a', 1: 'b', str: 'long string', nested: { nested: 0 }, hello: 55.5 }; for (let i = 10; i < 10000; i++) { obj[""+i] = "test" }
Tests:
Object.entries (forEach)
Object.entries(obj).forEach((key, value) => console.log(key, value));
for..in
for (let key in obj) { if (obj.hasOwnProperty(key)) console.log(key, obj[key]); }
Object.entries (for loop)
for (let entries = Object.entries(obj), i = entries.length - 1, entry = entries[i]; i >= 0; entry = entries[i--]) { console.log(entry[0], entry[1]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries (forEach)
for..in
Object.entries (for loop)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
13 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries (forEach)
20.4 Ops/sec
for..in
18.4 Ops/sec
Object.entries (for loop)
20.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. The provided JSON represents a JavaScript benchmark test case, specifically comparing three approaches to iterate over an object: 1. `Object.entries(obj).forEach((key, value) => console.log(key, value));` 2. `for (let key in obj) { if (obj.hasOwnProperty(key)) console.log(key, obj[key]); }` 3. `for (let entries = Object.entries(obj), i = entries.length - 1, entry = entries[i]; i >= 0; entry = entries[i--]) { console.log(entry[0], entry[1]) }` **Option Comparison:** * **`Object.entries()` with `forEach`**: This approach uses the `Object.entries()` method to get an array of key-value pairs and then uses the `forEach` method to iterate over each pair. The `forEach` method executes a provided callback function once for each element in the array. * **`for...in` loop**: This traditional loop iterates over the object's property names, checking if each property is an own property of the object using `obj.hasOwnProperty(key)`. It logs the key and its corresponding value to the console. * **`Object.entries()` with manual for loop**: This approach uses the same `Object.entries()` method as before but then manually iterates over the array of key-value pairs in a loop. It logs each key-value pair to the console. **Pros and Cons:** * **`Object.entries()` with `forEach`:** * Pros: * More concise and readable code. * No need to check if property is an own property using `obj.hasOwnProperty(key)`. * Cons: * Requires JavaScript version 16 or higher for `forEach` method on array literals. * **`for...in` loop:** * Pros: * Works across all supported JavaScript versions and environments. * No need to require additional libraries or modules. * Cons: * Less concise and readable code compared to other approaches. * Requires explicit checking for own properties using `obj.hasOwnProperty(key)`. * **`Object.entries()` with manual for loop:** * Pros: * Offers more control over the iteration process, allowing for fine-tuning of performance optimization techniques. * Works across all supported JavaScript versions and environments. * Cons: * Less concise and readable code compared to other approaches. **Library Usage:** None of the provided test cases use any external libraries or modules. The `Object.entries()` method is a built-in method in JavaScript, and the `for...in` loop is also a native JavaScript construct. **Special JS Features/Syntax:** The provided test case does not include any special features or syntax that require specific versions of JavaScript or specialized tools. **Alternatives:** If you need to benchmark different approaches for iterating over objects, here are some alternatives: * **Use the built-in `for...in` loop**: As mentioned earlier, this is a traditional loop that iterates over object property names. * **Utilize modern methods like `Object.keys()` and `Array.prototype.forEach()`**: These methods provide more concise and readable ways to iterate over objects, but may require specific JavaScript versions or environments. * **Use specialized libraries or frameworks for performance benchmarking**: If you need highly optimized benchmarks with fine-grained control, consider using specialized libraries or frameworks designed for performance benchmarking.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.entries vs Object.keys vs for...in
Object.keys().length vs for i in object i++ vs Object.entries().length vs Object.values().length
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?