Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs Object.entries Check
(version: 0)
Comparing performance of:
For in vs Object.entries
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {} for(i=0; i<10000; i++){ obj['key_'+i] = Math.random(); }
Tests:
For in
for(let key in obj){ if(obj[key]){ console.log(`${key}: ${obj[key]}`); } }
Object.entries
for (const [key, value] of Object.entries(obj)) { console.log(`${key}: ${value}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For in
Object.entries
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 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For in
20.6 Ops/sec
Object.entries
20.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for iterating over an object: `for...in` loop and `Object.entries()` method. The test case creates a large object (`obj`) with 10,000 properties, each initialized with a random value using `Math.random()`. The objective is to determine which approach is faster. **Script Preparation Code** The script preparation code sets up the benchmark by: 1. Creating an empty object (`var obj = {}`). 2. Populating the object with 10,000 properties using a `for` loop and assigning each property a random value. 3. The resulting object is stored in the `obj` variable. **Html Preparation Code** The html preparation code is not provided in this benchmark example, but it's likely that it would render an HTML page or perform some other initialization task to make the test environment more realistic. **Individual Test Cases** There are two individual test cases: 1. **For in**: This test case uses a `for...in` loop to iterate over the object properties. 2. **Object.entries**: This test case uses the `Object.entries()` method to get an array of key-value pairs and then iterate over it. **Library Usage** Neither of these test cases uses any external libraries. They rely solely on JavaScript's built-in features. **Special JS Features/Syntax** Both tests use standard JavaScript syntax. There are no special features or syntaxes being tested in this benchmark. **Comparison of Approaches** The two approaches differ in their approach to iterating over the object properties: * **For...in**: This approach iterates over the property names (i.e., keys) of the object, allowing for additional logic to be performed on each iteration. However, it can be slower due to the overhead of checking if a property is present (`if(obj[key])`). * **Object.entries**: This approach returns an array of key-value pairs, making it easier to iterate over both properties and values. However, it may require more memory to store the entire array. **Pros and Cons** Here's a summary of the pros and cons of each approach: * **For...in**: + Pros: Allows for additional logic on each iteration, can be faster in some cases. + Cons: Slower due to property existence checks. * **Object.entries**: + Pros: Easier to iterate over both properties and values, potentially faster due to reduced overhead. + Cons: Requires more memory to store the entire array. **Other Alternatives** If you're looking for alternative approaches, you could consider: * Using `for...of` loop with an iterator object, which can provide a similar syntax to `Object.entries`. * Using a library like Lodash or Ramda, which provides utility functions for iterating over objects. * Implementing your own custom iteration mechanism using techniques like iterators and generators. Keep in mind that the choice of approach ultimately depends on your specific use case and performance requirements.
Related benchmarks:
keys vs values
Object.entries VS Object.keys with obj[key]
For in vs Object.entries
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?