Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[key, value] of Object.entries vs for..in
(version: 0)
Comparing performance of:
[key, value] of Object.entries(obj) vs key in ob vs for i Object.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:
[key, value] of Object.entries(obj)
for (let i = 0; i < 10000; i++) { for (const [key, value] of Object.entries(obj)) { console.log(key, value); } }
key in ob
for (let i = 0; i < 10000; i++) { for (const key in obj) { const value = obj[key]; console.log(key, value); } }
for i Object.entries
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--]) { const [key, value] = entry; console.log(key, value); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
[key, value] of Object.entries(obj)
key in ob
for i Object.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):
Let's break down the provided benchmark JSON and explore what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark defines two approaches to iterate over an object: 1. `[key, value] of Object.entries`: This approach uses the `Object.entries` method to get an array of key-value pairs from the object, and then iterates over it using a `for...of` loop. 2. `for (const key in obj)`: This approach uses the `in` operator to iterate over the object's own enumerable properties (i.e., keys), and then accesses the corresponding value using the `obj[key]` syntax. **Comparison** The benchmark compares these two approaches, measuring their performance by executing each one 10,000 times. The goal is to determine which approach is faster. **Pros and Cons of Each Approach** 1. `[key, value] of Object.entries`: * Pros: This approach is more concise and readable, as it uses a standard JavaScript method (`Object.entries`) and a `for...of` loop. * Cons: Since `Object.entries` returns an array of key-value pairs, this approach might be slower than iterating directly over the object's keys. However, modern JavaScript engines are optimized for `Object.entries`, making this approach competitive. 2. `for (const key in obj)`: * Pros: This approach is potentially faster since it iterates directly over the object's keys without creating an array or using a loop. However, it relies on the `in` operator, which might lead to slower performance due to the overhead of resolving property names. * Cons: The syntax can be less readable and more error-prone than the `[key, value] of Object.entries` approach. **Library Used** In this benchmark, `Object.entries` is used as a built-in JavaScript method. There's no external library involved. **Special JS Feature or Syntax** The benchmark uses modern JavaScript features like: * `for...of` loops * Template literals (`\r\n`) * Object literal syntax (e.g., `var obj = {...}`) These features are widely supported by modern browsers, including Firefox 111. **Other Alternatives** If you want to explore alternative approaches or libraries for iterating over objects in JavaScript, consider the following options: * Using `for...in` loops instead of `for...of` loops * Utilizing the `forEach()` method on an array created from the object's keys (e.g., `[Object.keys(obj)]`) * Leveraging a library like Lodash or Ramda for functional programming approaches However, keep in mind that the built-in JavaScript methods (`Object.entries`, `for...of`) and loops are often optimized for performance and should be the primary choice for most use cases.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.entries vs Object.keys vs for...in
entries vs keys lookup
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?