Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ForIn vs ForEach
(version: 0)
Comparing performance of:
for in vs Object.keys vs Object.entries
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 10000; i++) { arr[i] = i; } function someFn(i) { return i * 2; }
Tests:
for in
for (var i in arr) { someFn(arr[i]); }
Object.keys
Object.keys(arr).forEach(i => someFn(arr[i]))
Object.entries
Object.entries(arr).forEach(i => someFn(i))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for in
Object.keys
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 and its test cases. **What is being tested?** The benchmark measures the performance of three different approaches for iterating over an array: 1. `for (var i in arr) { ... }` (also known as "For In") 2. `Object.keys(arr).forEach(i => someFn(arr[i]))` 3. `Object.entries(arr).forEach(i => someFn(i))` **Options being compared** These three approaches differ in how they iterate over the array and access its elements. * `for (var i in arr) { ... }`: This approach uses the "For In" loop, which iterates over the array's own enumerable properties. It returns a property value for each iteration. * `Object.keys(arr).forEach(i => someFn(arr[i]))`: This approach uses the `Object.keys()` method to get an array of the object's own enumerable property names. The `forEach` method is then used to iterate over this array, and for each property name, it calls a function with the corresponding array element. * `Object.entries(arr).forEach(i => someFn(i))`: This approach uses the `Object.entries()` method to get an array of key-value pairs from the object. The `forEach` method is then used to iterate over this array, and for each entry, it calls a function with the key and value. **Pros and Cons** Here are some pros and cons of each approach: * `for (var i in arr) { ... }`: + Pros: Simple, intuitive syntax; easy to understand. + Cons: Can be slower due to the use of property iteration, which can lead to additional overhead. * `Object.keys(arr).forEach(i => someFn(arr[i]))`: + Pros: More explicit and controlled iteration; can be faster for large arrays. + Cons: Requires an extra method call (`Object.keys()`), which may add overhead. * `Object.entries(arr).forEach(i => someFn(i))`: + Pros: Even more explicit and controlled iteration; can be faster for large arrays. + Cons: Requires an additional method call (`Object.entries()`), which may add overhead. **Library and purpose** In the provided benchmark, no specific library is used. However, `Object.keys()` and `Object.entries()` are built-in methods of JavaScript objects that provide a way to iterate over their properties. **Special JS feature or syntax** No special JavaScript features or syntax are mentioned in the provided code snippet.
Related benchmarks:
index loop vs for-of loop vs foreach vs map
Array loop vs foreach vs map (Small arrays)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Array loop vs foreach vs for_of
Comments
Confirm delete:
Do you really want to delete benchmark?