Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Consume Object.entries vs for..in vs for..in fn 222222
(version: 0)
Comparing performance of:
Object.entries vs for..in vs entries
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) { console.log([key, obj[key]]) } }
entries
for (let i = 0; i < 10000; i++) { for (const [key, entry] of Object.entries(obj)) { console.log([key, entry]) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
for..in
entries
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):
Measuring the performance of JavaScript loops can be challenging due to various factors like browser optimizations, cache effects, and hardware variability. However, understanding what's being tested is essential for making sense of the results. **Benchmark Overview** The provided benchmark compares three ways to iterate over an object's properties: 1. `Object.entries`: This method returns an array of key-value pairs. 2. `for...in`: This loop iterates over the object's property names, not values. 3. `entries` (a custom alias for `Object.entries`): This is a variation using destructuring. **Comparison and Analysis** The three approaches differ in how they handle object iteration: * **`Object.entries`**: Returns an array of key-value pairs, where each pair contains the property name and value. The loop iterates over this array, allowing for direct access to both properties. * **`for...in`**: Iterates over the object's property names only (not values), using the property name as a variable in the loop. * **`entries`** (alias for `Object.entries`): Similar to `Object.entries`, but uses destructuring to extract key and value variables from each pair. Now, let's examine the pros and cons of each approach: * **`Object.entries`**: * Pros: Direct access to both properties in each iteration, can use destructuring for concise variable declarations. * Cons: May incur additional overhead due to array creation and manipulation. * **`for...in`**: * Pros: Simple, intuitive loop structure with minimal overhead. * Cons: Only iterates over property names (not values), which may lead to unnecessary checks or assumptions about property existence. Considering the pros and cons, `Object.entries` generally offers a better performance profile compared to `for...in`, as it allows for direct access to both properties. However, its overhead due to array creation might be significant if executed in large numbers. The `entries` alias adds minimal value but also incurs some overhead. **Library Considerations** None of the provided benchmarks rely on external libraries, which simplifies the test cases and makes them more portable. **Special JS Features/Syntax** There are no specific ES6+ features or syntax used in these benchmark definitions. They focus solely on object iteration techniques. **Alternative Options** If not using `Object.entries`, other alternatives for iterating over objects include: * **`for...in`**: As mentioned earlier, iterates over property names only. * **`for...of`**: Iterates over the values of an array-like or iterable object (not directly applicable to objects). * **`forEach`**: Iterates over arrays or iterable objects (not directly applicable to objects).
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?