Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Obj iteration v2
(version: 1)
Comparing performance of:
Iterate entries vs Iterate keys vs Iterate keys use "of syntax"
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
Iterate entries
const obj = { 'a1': 'fooo', 'a2': 'fooo', 'a3': 'fooo', 'a4': 'fooo', 'a5': 'fooo', 'a6': 'fooo', 'a7': 'fooo', 'a8': 'fooo', 'a9': 'fooo', 'a11': 'fooo', 'a12': 'fooo', 'a13': 'fooo', 'a14': 'fooo', 'a15': 'fooo', 'a16': 'fooo', 'a17': 'fooo', 'a18': 'fooo', 'a19': 'fooo', 'a20': 'fooo' }; const values = []; for (const [key, value] of Object.entries(obj)) { values.push(key + ':' + value); }
Iterate keys
const obj = { 'a1': 'fooo', 'a2': 'fooo', 'a3': 'fooo', 'a4': 'fooo', 'a5': 'fooo', 'a6': 'fooo', 'a7': 'fooo', 'a8': 'fooo', 'a9': 'fooo', 'a11': 'fooo', 'a12': 'fooo', 'a13': 'fooo', 'a14': 'fooo', 'a15': 'fooo', 'a16': 'fooo', 'a17': 'fooo', 'a18': 'fooo', 'a19': 'fooo', 'a20': 'fooo' }; const values = []; const keys = Object.keys(obj).length; for (let i=1;i<keys.length;i++) { values.push(keys[i] + ':' + obj[keys[i]]); }
Iterate keys use "of syntax"
const obj = { 'a1': 'fooo', 'a2': 'fooo', 'a3': 'fooo', 'a4': 'fooo', 'a5': 'fooo', 'a6': 'fooo', 'a7': 'fooo', 'a8': 'fooo', 'a9': 'fooo', 'a11': 'fooo', 'a12': 'fooo', 'a13': 'fooo', 'a14': 'fooo', 'a15': 'fooo', 'a16': 'fooo', 'a17': 'fooo', 'a18': 'fooo', 'a19': 'fooo', 'a20': 'fooo' }; const values = []; for (const key of Object.keys(obj)) { values.push(key + ':' + obj[key]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Iterate entries
Iterate keys
Iterate keys use "of syntax"
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 YaBrowser/24.1.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Iterate entries
659088.9 Ops/sec
Iterate keys
6535406.5 Ops/sec
Iterate keys use "of syntax"
1156692.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark you provided measures the performance of iterating over object entries or keys using different approaches. **Test Cases** There are three test cases: 1. **Iterate Entries**: This test case creates an object with 20 properties, iterates over its entries using a `for...of` loop, and pushes each key-value pair to an array. 2. **Iterate Keys**: This test case creates the same object as above, but iterates over its keys using a traditional `for` loop. 3. **Iterate Keys use "of syntax"**: This test case is similar to the previous one, but uses the new `"of"` syntax for iterating over object keys. **Options Compared** The three test cases compare the performance of: 1. Iterating over object entries using a `for...of` loop (Test Case 1) 2. Iterating over object keys using a traditional `for` loop (Test Case 2) 3. Iterating over object keys using the new `"of"` syntax (Test Case 3) **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Iterate Entries (`for...of` loop)**: + Pros: concise, readable, and efficient. + Cons: may be less performant due to string concatenation inside the loop. * **Iterate Keys (traditional `for` loop)**: + Pros: straightforward, easy to understand, and potentially faster than `"of"` syntax. + Cons: more verbose and less readable than `for...of` loop. * **Iterate Keys use "of syntax"**: + Pros: concise, readable, and efficient (similar to `for...of` loop). + Cons: relatively new syntax, may not be supported by older browsers. **Library/Functions Used** The test cases do not explicitly mention any libraries or functions. However, they utilize built-in JavaScript features like: * `Object.entries()`: returns an array of a given object's key-value pairs. * `Object.keys()`: returns an array of a given object's keys. * String concatenation (`+`) inside loops. **Special JS Features/Syntax** The test cases use the following special JavaScript features/syntax: * **`for...of` loop**: used for iterating over iterable objects (e.g., arrays, sets). * **New `"of"` syntax**: introduced in ECMAScript 2019, allows for concise iteration over object keys. **Other Alternatives** If you're interested in exploring other approaches or variations on these test cases, here are a few suggestions: * Use `forEach()` instead of loops to iterate over the array of key-value pairs. * Compare performance using different data structures (e.g., objects with 10 vs. 100 properties). * Experiment with older browsers that may not support `"of"` syntax. I hope this helps you understand the benchmark better!
Related benchmarks:
Object iteration vs Map iteration
Object iteration vs Map iteration V2
Object iteration vs Map iteration with lambda functions
Object iteration vs Map iteration with deep cloning
Object iteration vs Map iteration 2
Comments
Confirm delete:
Do you really want to delete benchmark?