Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Consume Object.entries vs for..in vs for..in fn 22222s
(version: 0)
Comparing performance of:
Object.entries vs for..in vs Object.keys vs for..in no array
Created:
4 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++) { Object.entries(obj).forEach((entry) => console.log(obj[entry[0]])); }
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++) { Object.keys(obj).forEach(key => console.log(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]) } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.entries
for..in
Object.keys
for..in 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):
I'll break down what's being tested in the provided JSON and explain the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is defined by two main scripts: `Script Preparation Code` and `Html Preparation Code`. The `Script Preparation Code` defines an object `obj` with nested properties, including a property named `0` (which is not a valid key in JavaScript). This setup is likely chosen to test the behavior of each method on objects with non-standard keys. **Test Cases** There are four individual test cases: 1. **Object.entries**: This method returns an array of tuples containing the keys and values of the object. 2. **for..in**: This loop iterates over the properties of an object using their names as strings. 3. **Object.keys**: This method returns an array of the object's own property keys. 4. **for..in no array**: Similar to the previous one, but without using `Array.prototype.forEach()`. **Options Compared** The benchmark is comparing the performance of: * Using `Object.entries` with the `forEach()` method * Using `for...in` loop with `if (obj.hasOwnProperty(key))` * Using `Object.keys()` and iterating over it with another `for...in` loop or `Array.prototype.forEach()` **Pros and Cons** 1. **Object.entries**: Pros: * More concise and expressive syntax * Does not require explicit key iteration Cons: * May be slower due to the overhead of creating an array and using `forEach()` 2. **for...in**: Pros: * Well-established and widely supported * Can be faster since it avoids the overhead of creating an array Cons: * More verbose syntax * Does not handle non-standard keys (like `0`) well, as shown in the benchmark setup 3. **Object.keys**: Pros: * Similar performance to `for...in` loop when combined with `Array.prototype.forEach()` Cons: * Requires additional method call and array creation overhead **Other Considerations** * The use of a non-standard key (`0`) in the object setup may skew the results, as it can lead to unexpected behavior in older browsers or implementations. * The presence of a nested object (`nested: { ... }`) may affect performance due to increased memory allocation and garbage collection. **Alternative Approaches** Other alternatives for iterating over an object's properties include: * Using `Object.keys()` and iterating over its returned array directly, without using `Array.prototype.forEach()` * Implementing a custom iterator or generator function * Using libraries like Lodash or Ramda that provide optimized iteration methods Keep in mind that the best approach often depends on the specific use case, performance requirements, and browser support considerations.
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?