Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of, for..in, for, map, forEach
(version: 0)
Comparing performance of:
For of vs map vs forEach vs for in
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = new Array(100).fill(1); const obj = { ...arr.reduce((acc, curr) => { acc[curr] = curr; return acc; }, {}) }
Tests:
For of
const arr = new Array(100).fill(1); for (const item of arr) { console.log(item) }
map
const arr = new Array(100).fill(1); arr.map(console.log)
forEach
const arr = new Array(100).fill(1); arr.forEach(console.log)
for in
const arr = new Array(100).fill(1); const obj = { ...arr.reduce((acc, curr) => { acc[curr] = curr; return acc; }, {}) } for (const item in obj) { console.log(obj[item]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
For of
map
forEach
for in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For of
3295.2 Ops/sec
map
692.7 Ops/sec
forEach
695.7 Ops/sec
for in
249938.0 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 provided benchmark definition json represents a set of test cases designed to compare the performance of different JavaScript iteration methods: `for..of`, `for..in`, `for`, `map`, and `forEach`. **Test Case Breakdown** The individual test cases are: 1. **For of**: Iterates over an array using the `for...of` loop, logging each item to the console. 2. **Map**: Applies a callback function to each element of an array using the `map()` method, logging each result to the console. 3. **ForEach**: Applies a callback function to each element of an array using the `forEach()` method, logging each result to the console. 4. **For in**: Iterates over an object using the `for...in` loop, logging each property value to the console. **Library Usage** The test cases use the following libraries: * None (no external libraries are required for these tests) **Special JavaScript Features or Syntax** None of the test cases rely on special JavaScript features or syntax beyond what is built-in to the language. However, it's worth noting that the `for...of` loop and the `map()` method both take advantage of modern JavaScript's iterator protocol. **Options Comparison** The benchmark compares different iteration methods with varying performance characteristics: * **For..of**: Designed for iterating over arrays using a modern, concise syntax. It is generally faster than other iteration methods. * **For in**: Iterates over objects, which can be slower than array iteration due to the overhead of property enumeration. * **For**: A traditional loop that requires manual indexing and looping control. This method is often slower than `for...of` and `map()`. * **Map**: Applies a transformation function to each element of an array, returning a new array with the results. It can be faster than using `forEach()` for simple transformations. **Pros and Cons** Here's a brief summary of the pros and cons of each iteration method: * **For..of**: + Pros: Fast, concise syntax, suitable for arrays. + Cons: Limited to iterating over arrays; may not work with other iterable objects. * **For in**: + Pros: Works with both arrays and objects; simple syntax. + Cons: Slower than array iteration methods; relies on property enumeration, which can be slower. * **For**: + Pros: General-purpose loop that works with any type of object. + Cons: Slow due to manual indexing and looping control; verbose syntax. * **Map**: + Pros: Fast for simple transformations; returns a new array, avoiding side effects. + Cons: May incur additional memory allocation overhead; requires explicit transformation function. **Other Alternatives** For iterating over arrays or objects, other alternatives include: * Using `forEach()` instead of `for...of`, which is also designed for array iteration but does not provide the same level of concision as `for...of`. * Utilizing `reduce()`, which can be used to accumulate values in an array or object using a callback function. * Considering other libraries or frameworks that offer optimized iteration methods, such as `loople` or `iterable utilities`.
Related benchmarks:
Basic Array processing benchmark
my Benchmark
Test array reduce
for of vs map with object
Comments
Confirm delete:
Do you really want to delete benchmark?