Which JavaScript Loop Is Fastest? Benchmark Results Revealed
This article benchmarks various JavaScript looping constructs—including for, while, do‑while, for‑of, forEach, map, filter, reduce, some, every, and find—by moving 50 million items between arrays and measuring execution time to determine the fastest approaches.
Loops are a built‑in feature of any programming language, and JavaScript provides many ways to iterate over data. This article tests several JavaScript looping methods to find out which is the fastest.
For Loop
While Loop
Do‑While Loop
For‑In Loop (excluded from the test)
For‑Of Loop
forEach Loop
Map Loop
Filter Loop
Reduce Loop
Some Loop
Every Loop
Find Loop
Performance is measured with console.time() and console.timeEnd() . The test task moves 50 million items from one array to another, and each loop runs asynchronously to ensure a fair comparison.
<code>console.time('My Description');
// Code to measure
console.timeEnd('My Description');</code>The array used for the test is created as follows:
<code>console.time('Array Creation');
const numbersList = Array.from({ length: 50_000_000 }, () => Math.floor(Math.random() * 100));
console.timeEnd('Array Creation');</code>All loops are executed in an async IIFE:
<code>(async () => {
await usingForLoop(numbersList);
await usingWhile(numbersList);
await usingDoWhile(numbersList);
await usingForOf(numbersList);
await usingForEach(numbersList);
await usingMap(numbersList);
await usingFilter(numbersList);
await usingReduce(numbersList);
await usingSome(numbersList);
await usingEvery(numbersList);
await usingFind(numbersList);
})();</code>Loop Implementations
For Loop
<code>const usingForLoop = async (array) => {
console.time('FOR LOOP');
const newNumbersList = [];
for (let i = 0; i < array.length; i++) {
newNumbersList.push(array[i]);
}
console.timeEnd('FOR LOOP');
};</code>While Loop
<code>const usingWhile = async (array) => {
console.time('WHILE');
let i = 0;
const newNumbersList = [];
while (i < array.length) {
newNumbersList.push(array[i]);
i++;
}
console.timeEnd('WHILE');
};</code>Do‑While Loop
<code>const usingDoWhile = async (array) => {
console.time('DO WHILE');
let i = 0;
const newNumbersList = [];
do {
newNumbersList.push(array[i]);
i++;
} while (i < array.length);
console.timeEnd('DO WHILE');
};</code>For‑Of Loop
<code>const usingForOf = async (array) => {
console.time('FOR OF');
const newNumbersList = [];
for (const item of array) {
newNumbersList.push(item);
}
console.timeEnd('FOR OF');
};</code>forEach Loop
<code>const usingForEach = async (array) => {
console.time('FOR EACH');
const newNumbersList = [];
array.forEach((item) => newNumbersList.push(item));
console.timeEnd('FOR EACH');
};</code>Map Loop
<code>const usingMap = async (array) => {
console.time('MAP');
const newNumbersList = array.map((number) => number);
console.timeEnd('MAP');
};</code>Filter Loop
<code>const usingFilter = async (array) => {
console.time('FILTER');
const newNumbersList = array.filter(() => true);
console.timeEnd('FILTER');
};</code>Reduce Loop
<code>const usingReduce = async (array) => {
console.time('REDUCE');
const newNumbersList = array.reduce((acc, item) => {
acc.push(item);
return acc;
}, []);
console.timeEnd('REDUCE');
};</code>Some Loop
<code>const usingSome = async (array) => {
console.time('SOME');
const newNumbersList = [];
array.some((item) => {
newNumbersList.push(item);
return false;
});
console.timeEnd('SOME');
};</code>Every Loop
<code>const usingEvery = async (array) => {
console.time('EVERY');
const newNumbersList = [];
array.every((item) => {
newNumbersList.push(item);
return true;
});
console.timeEnd('EVERY');
};</code>Find Loop
<code>const usingFind = async (array) => {
console.time('FIND');
const newNumbersList = [];
array.find((item) => {
newNumbersList.push(item);
return false;
});
console.timeEnd('FIND');
};</code>Each loop was executed five times and the average timings were recorded. The resulting chart (shown below) indicates the top five fastest methods:
Map
For Loop
While
Do While
For Each
Interestingly, only Map is a function call; the others are explicit loop constructs. The test focused on a single task, so results may vary with different workloads, memory, or CPU configurations. Nevertheless, Map and For Loop consistently showed the best performance, while For‑Of performed surprisingly poorly.
Although Map invokes a callback each iteration, modern JavaScript engines such as V8 heavily optimize high‑order array methods. These optimizations can reduce overhead and predict execution paths, allowing Map to rival or even surpass a traditional for loop.
Conclusion
Based on the benchmark, Map and For Loop have comparable efficiency. Choose the construct that best fits your needs: Map returns a new array but cannot break early, whereas a for loop offers more control.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.