Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Consume Object.entries vs for..in 2
(version: 8)
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
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {}; const aCode = 'a'.charCodeAt(0); const charsQty = 'z'.charCodeAt(0) - aCode; for (let i = 0; i < charsQty; i += 1) { const key0 = String.fromCharCode(aCode + i); for (let j = 0; j < charsQty; j += 1) { const key1 = key0 + String.fromCharCode(aCode + j); for (let k = 0; k < charsQty; k += 1) { const key2 = key1 + String.fromCharCode(aCode + k); obj[key2] = key2; } } }
Tests:
Object.entries
for (let entries = Object.entries(obj), i = entries.length - 1; i >= 0; i--) { console.log(entries[i]) }
for..in
for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log([key, obj[key]]) } }
Object.keys
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 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 key in obj) { if (obj.hasOwnProperty(key)) { console.log(key, obj[key]) } }
Object.entries no array
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 (6)
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
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):
**Benchmark Overview** The MeasureThat.net benchmark measures the performance of different ways to iterate over an object's entries in JavaScript. The benchmark consists of two parts: script preparation code and HTML preparation code (which is empty in this case). The script preparation code creates a large object with nested keys, while the HTML preparation code is not used. **Script Preparation Code** The script preparation code generates a large object `obj` with nested keys using a complex loop structure. The object has 3 nested levels of keys, resulting in approximately 3^3 = 27 unique keys. The code then uses three different methods to iterate over the object's entries: 1. **Object.entries**: Iterates over an array of key-value pairs. 2. **for..in**: Iterates over the object's own enumerable properties (keys). 3. **Object.keys** and its variants: Iterates over an array of keys. Each method is used in a separate test case, allowing users to compare their performance. **Iteration Methods Comparison** Here's a brief overview of each iteration method: 1. **Object.entries**: This method returns an array of key-value pairs from the object. It uses the object's `entries` property, which is a built-in method that returns an iterator over the object's own enumerable entries. * Pros: Fast and efficient, as it uses the built-in `entries` property. * Cons: May not be suitable for objects with large numbers of non-enumerable properties. 2. **for..in**: This method iterates over the object's own enumerable properties (keys) using a loop. * Pros: Can be used when the object has many non-enumerable properties, as it only iterates over the enumerable ones. * Cons: May not be suitable for objects with large numbers of keys, as it uses a loop and may be slower than Object.entries. 3. **Object.keys** and its variants: These methods return an array of keys from the object. There are two variants: * `Object.keys(obj)`: Returns an array of all keys in the object. * `for (let key in obj) {\r\n if (obj.hasOwnProperty(key)) {\r\n console.log([key, obj[key]])\r\n }\r\n}`: Iterates over the object's own enumerable properties (keys) using a loop and checks if each property is an own property of the object. **Other Considerations** * The benchmark uses a Linux x86_64 browser (Chrome 101) to run the tests, which may not be representative of other platforms or browsers. * The number of executions per second is used as the performance metric, but it's unclear if this is a suitable metric for all use cases. **Alternatives** Other alternatives to these iteration methods include: 1. **Array.prototype.forEach**: Can be used to iterate over an array of keys from the object. 2. **for...of**: Can be used to iterate over an iterable object, such as an array or set. 3. **Symbol.iterator**: Can be used to create a custom iterator for an object. Note that these alternatives may have different performance characteristics and use cases compared to the methods in this benchmark.
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 Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?