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.
console.time('My Description');
// Code to measure
console.timeEnd('My Description');The array used for the test is created as follows:
console.time('Array Creation');
const numbersList = Array.from({ length: 50_000_000 }, () => Math.floor(Math.random() * 100));
console.timeEnd('Array Creation');All loops are executed in an async IIFE:
(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);
})();Loop Implementations
For Loop
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');
};While Loop
const usingWhile = async (array) => {
console.time('WHILE');
let i = 0;
const newNumbersList = [];
while (i < array.length) {
newNumbersList.push(array[i]);
i++;
}
console.timeEnd('WHILE');
};Do‑While Loop
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');
};For‑Of Loop
const usingForOf = async (array) => {
console.time('FOR OF');
const newNumbersList = [];
for (const item of array) {
newNumbersList.push(item);
}
console.timeEnd('FOR OF');
};forEach Loop
const usingForEach = async (array) => {
console.time('FOR EACH');
const newNumbersList = [];
array.forEach((item) => newNumbersList.push(item));
console.timeEnd('FOR EACH');
};Map Loop
const usingMap = async (array) => {
console.time('MAP');
const newNumbersList = array.map((number) => number);
console.timeEnd('MAP');
};Filter Loop
const usingFilter = async (array) => {
console.time('FILTER');
const newNumbersList = array.filter(() => true);
console.timeEnd('FILTER');
};Reduce Loop
const usingReduce = async (array) => {
console.time('REDUCE');
const newNumbersList = array.reduce((acc, item) => {
acc.push(item);
return acc;
}, []);
console.timeEnd('REDUCE');
};Some Loop
const usingSome = async (array) => {
console.time('SOME');
const newNumbersList = [];
array.some((item) => {
newNumbersList.push(item);
return false;
});
console.timeEnd('SOME');
};Every Loop
const usingEvery = async (array) => {
console.time('EVERY');
const newNumbersList = [];
array.every((item) => {
newNumbersList.push(item);
return true;
});
console.timeEnd('EVERY');
};Find Loop
const usingFind = async (array) => {
console.time('FIND');
const newNumbersList = [];
array.find((item) => {
newNumbersList.push(item);
return false;
});
console.timeEnd('FIND');
};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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
