Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Consume Object.entries vs for..in vs for..in fn 22223
(version: 0)
Comparing performance of:
Object.entries vs for..in vs Object.keys vs Object.keys no entry vs for..in no array vs Object.entries no array vs Object.entries #2
Created:
3 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 };
Tests:
Object.entries
for (let i = 0; i < 10000; i++) { for (let entries = Object.entries(obj), i = entries.length - 1; i >= 0; i--) { console.log(entries[i]) } }
for..in
for (let i = 0; i < 10000; i++) { for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log([key, obj[key]]) } } }
Object.keys
for (let i = 0; i < 10000; i++) { for (let keyArray = Object.keys(obj), i = keyArray.length - 1, key = keyArray[i]; i >= 0; key = keyArray[--i]) { console.log([key, obj[key]]); } }
Object.keys no entry
for (let i = 0; i < 10000; i++) { for (let keyArray = Object.keys(obj), i = keyArray.length - 1, key = keyArray[i]; i >= 0; key = keyArray[--i]) { console.log(key, obj[key]); } }
for..in no array
for (let i = 0; i < 10000; i++) { for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log(key, obj[key]) } } }
Object.entries no array
for (let i = 0; i < 10000; i++) { for (let entries = Object.entries(obj), i = entries.length - 1, entry = entries[i]; i >= 0; entry = entries[i--]) { console.log(entry[0], entry[1]) } }
Object.entries #2
for (let i = 0; i < 10000; i++) { for (const entry of Object.entries(obj)) { console.log(entry) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
Object.entries
for..in
Object.keys
Object.keys no entry
for..in no array
Object.entries no array
Object.entries #2
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 benchmark and its various components. **Benchmark Overview** The benchmark compares the performance of four different approaches to iterate over an object: 1. `Object.entries` 2. `for..in` (without using `Object.entries`) 3. `Object.keys` (without using `Object.entries`) 4. A variation of `Object.keys` without printing the key-value pairs **Options Compared** The benchmark compares the performance of each approach in terms of the number of executions per second. * `Object.entries`: Iterates over the object's own enumerable properties as an array of [key, value] pairs. * `for..in`: Iterates over the object's own enumerable properties using a traditional for loop. This approach requires manual management of the property iteration process, including checking for existence and accessing the value. * `Object.keys`: Returns an array of the object's own enumerable property names. **Pros and Cons** 1. **`Object.entries`**: * Pros: Fast and efficient, as it uses a built-in function to iterate over the object's properties. * Cons: Requires a modern JavaScript engine (e.g., ECMAScript 2015+) that supports the `Object.entries()` method. 2. **`for..in`**: * Pros: Widely supported across older browsers and engines, as it doesn't rely on the `Object.entries()` method or other advanced features. * Cons: Requires manual property iteration management, which can be error-prone and slower than using built-in functions like `Object.entries()`. 3. **`Object.keys`**: * Pros: Similar to `Object.entries`, but without the array of key-value pairs, making it more suitable for scenarios where only the keys are needed. * Cons: Requires a modern JavaScript engine (e.g., ECMAScript 2015+) that supports the `Object.keys()` method. **Performance Variations** The benchmark shows varying performance results across different browsers and engines. This is due to factors like: * Engine optimizations for specific features (e.g., `Object.entries()` vs. `for..in`) * Browser-specific performance characteristics (e.g., Firefox's performance on Mac OS X) * Differences in the JavaScript engine's execution speed **Insights** The benchmark highlights the importance of choosing the most suitable approach for a particular use case, considering factors like: * Performance requirements * Compatibility with different browsers and engines * Code readability and maintainability
Related benchmarks:
For in vs For of
Object.entries vs Object.keys vs for...in
Object entries vs forin
in vs not undefined
JS instanceof vs in
Comments
Confirm delete:
Do you really want to delete benchmark?