Skip to content

Instantly share code, notes, and snippets.

@DungGramer
Last active October 16, 2023 06:40
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DungGramer/7efdfefecaa1b8f5d6510202524dc751 to your computer and use it in GitHub Desktop.
Save DungGramer/7efdfefecaa1b8f5d6510202524dc751 to your computer and use it in GitHub Desktop.
Find fastest way to loop through an array in Javascript
const {performance} = require('perf_hooks');
// Create 1000 random numbers
const arr = length =>
Array.from({length}, () => Math.floor(Math.random() * 1000));
function record(arr) {
const result = [];
function timeRun(callback, message) {
const start = performance.now();
callback();
const end = performance.now();
result.push({
type: message,
time: end - start,
});
}
timeRun(() => {
for (let i = 0, length = arr.length; i < length; i++) {}
}, 'for ++');
timeRun(() => {
for (let i = 0, length = arr.length; i < length; ++i) {}
}, '++ for');
timeRun(() => {
for (let i = 0, length = arr.length; i !== length; i++) {}
}, 'for !== ++');
timeRun(() => {
for (let i = 0, length = arr.length; i !== length; ++i) {}
}, '++ for !==');
timeRun(() => {
for (let length = arr.length, i = length - 1; i >= 0; i--) {}
}, 'for --');
timeRun(() => {
for (let length = arr.length, i = length - 1; i >= 0; --i) {}
}, '-- for');
timeRun(() => {
for (let i = arr.length; i--; ) {}
}, 'for i length --');
timeRun(() => {
for (let i = arr.length; --i; ) {}
}, '--for i length');
timeRun(() => {
arr.map(i => i);
}, 'map');
timeRun(() => {
for (const i of arr) {
}
}, 'for of');
timeRun(() => {
arr.flatMap(i => [i]);
}, 'flatMap');
timeRun(() => {
for (const i in arr) {
Object.prototype.hasOwnProperty.call(arr, i);
}
}, 'for in');
timeRun(() => {
arr.forEach(() => {});
}, 'forEach');
timeRun(() => {
[].forEach.call(arr, () => {});
}, 'forEach call');
timeRun(() => {
Array.from(arr, () => {});
}, 'Array from');
timeRun(() => {
Object.keys(arr).map(i => i);
}, 'Object.keys map');
timeRun(() => {
let length = arr.length;
while (length--) {}
}, 'while forward --');
timeRun(() => {
let length = arr.length;
while (--length) {}
}, '--while forward');
timeRun(() => {
let length = arr.length;
do {
length--;
} while (length);
}, 'do while forward --');
timeRun(() => {
let length = arr.length;
do {
--length;
} while (length);
}, '--do while forward');
timeRun(() => {
let i = 0;
while (i < arr.length) {
i++;
}
}, 'while ++');
timeRun(() => {
let i = 0;
while (i < arr.length) {
++i;
}
}, '++ while');
timeRun(() => {
let i = 0;
do {
i++;
} while (i < arr.length);
}, 'do while ++');
timeRun(() => {
let i = 0;
do {
++i;
} while (i < arr.length);
}, '++ do while');
timeRun(() => {
let i = arr.length;
while (--i >= 0) {}
}, '--while >= 0');
timeRun(() => {
while (arr.shift()) {}
}, 'while shift');
const arrClone = [...arr];
timeRun(() => {
for (let i; (i = arrClone.pop()); ) {}
}, 'pop');
result.sort((a, b) => a.time - b.time);
console.log(`Best: ${result[0].type}: ${result[0].time}ms`);
console.table(result);
}
console.log('arr with 100 elements'); // --while forward: 0.013000000268220901ms
record(arr(100));
console.log('arr with 1000 elements'); // do while forward --: 0.006699997931718826ms
record(arr(1000));
console.log('arr with 10_000 elements'); // pop: 0.0020000003278255463ms
record(arr(10_000));
console.log('arr with 100_000 elements'); // pop: 0.012400001287460327ms
record(arr(100_000));
console.log('arr with 1_000_000 elements'); // pop: 0.01029999926686287ms
record(arr(1_000_000));
console.log('arr with 10_000_000 elements'); // pop: 0.007999997586011887ms
record(arr(10_000_000));
arr with 100 elements
Best: --while forward: 0.01289999857544899ms
┌─────────┬───────────────────────┬──────────────────────┐
│ (index) │ type │ time │
├─────────┼───────────────────────┼──────────────────────┤
│ 0 │ '--while forward' │ 0.01289999857544899 │
│ 1 │ '--do while forward' │ 0.012900002300739288 │
│ 2 │ 'do while forward --' │ 0.013699997216463089 │
│ 3 │ '--while >= 0' │ 0.014499999582767487 │
│ 4 │ '--for i length' │ 0.014800000935792923 │
│ 5 │ 'while forward --' │ 0.01599999889731407 │
│ 6 │ '++ do while' │ 0.01599999889731407 │
│ 7 │ 'while ++' │ 0.017400000244379044 │
│ 8 │ '++ for !==' │ 0.01770000159740448 │
│ 9 │ 'for --' │ 0.018299996852874756 │
│ 10 │ 'do while ++' │ 0.019099999219179153 │
│ 11 │ '++ while' │ 0.019800003618001938 │
│ 12 │ 'for !== ++' │ 0.020399998873472214 │
│ 13 │ 'pop' │ 0.02070000022649765 │
│ 14 │ '++ for' │ 0.020799998193979263 │
│ 15 │ '-- for' │ 0.022199999541044235 │
│ 16 │ 'forEach' │ 0.024299997836351395 │
│ 17 │ 'forEach call' │ 0.026500001549720764 │
│ 18 │ 'map' │ 0.0268000029027462 │
│ 19 │ 'Array from' │ 0.030199997127056122 │
│ 20 │ 'while shift' │ 0.03179999813437462 │
│ 21 │ 'for ++' │ 0.03229999914765358 │
│ 22 │ 'for of' │ 0.032999999821186066 │
│ 23 │ 'Object.keys map' │ 0.03349999710917473 │
│ 24 │ 'for i length --' │ 0.04050000011920929 │
│ 25 │ 'for in' │ 0.05169999971985817 │
│ 26 │ 'flatMap' │ 0.08609999716281891 │
└─────────┴───────────────────────┴──────────────────────┘
arr with 1000 elements
Best: pop: 0.0018000006675720215ms
┌─────────┬───────────────────────┬───────────────────────┐
│ (index) │ type │ time │
├─────────┼───────────────────────┼───────────────────────┤
│ 0 │ 'pop' │ 0.0018000006675720215 │
│ 1 │ '--do while forward' │ 0.006999999284744263 │
│ 2 │ '--for i length' │ 0.007000003010034561 │
│ 3 │ 'do while forward --' │ 0.007000003010034561 │
│ 4 │ '--while forward' │ 0.008500002324581146 │
│ 5 │ '-- for' │ 0.009599998593330383 │
│ 6 │ 'for --' │ 0.00989999994635582 │
│ 7 │ 'for !== ++' │ 0.010000001639127731 │
│ 8 │ 'for i length --' │ 0.010099999606609344 │
│ 9 │ '++ for' │ 0.010199997574090958 │
│ 10 │ '--while >= 0' │ 0.010400000959634781 │
│ 11 │ '++ for !==' │ 0.011500000953674316 │
│ 12 │ 'while forward --' │ 0.012800000607967377 │
│ 13 │ '++ do while' │ 0.014399997889995575 │
│ 14 │ 'do while ++' │ 0.014800000935792923 │
│ 15 │ '++ while' │ 0.01510000228881836 │
│ 16 │ 'forEach call' │ 0.018699999898672104 │
│ 17 │ 'map' │ 0.025200001895427704 │
│ 18 │ 'forEach' │ 0.031199999153614044 │
│ 19 │ 'while ++' │ 0.03420000150799751 │
│ 20 │ 'Object.keys map' │ 0.0356999970972538 │
│ 21 │ 'for ++' │ 0.036399997770786285 │
│ 22 │ 'for of' │ 0.03969999775290489 │
│ 23 │ 'Array from' │ 0.04600000008940697 │
│ 24 │ 'while shift' │ 0.16759999841451645 │
│ 25 │ 'for in' │ 0.190700002014637 │
│ 26 │ 'flatMap' │ 0.35999999940395355 │
└─────────┴───────────────────────┴───────────────────────┘
arr with 10_000 elements
Best: pop: 0.03959999978542328ms
┌─────────┬───────────────────────┬─────────────────────┐
│ (index) │ type │ time │
├─────────┼───────────────────────┼─────────────────────┤
│ 0 │ 'pop' │ 0.03959999978542328 │
│ 1 │ '--do while forward' │ 0.05810000002384186 │
│ 2 │ 'do while forward --' │ 0.05819999799132347 │
│ 3 │ '--for i length' │ 0.05999999865889549 │
│ 4 │ '--while forward' │ 0.06940000131726265 │
│ 5 │ '--while >= 0' │ 0.08300000056624413 │
│ 6 │ 'while forward --' │ 0.08510000258684158 │
│ 7 │ '-- for' │ 0.08860000222921371 │
│ 8 │ '++ for !==' │ 0.09200000017881393 │
│ 9 │ 'for !== ++' │ 0.09459999948740005 │
│ 10 │ 'for --' │ 0.1015000008046627 │
│ 11 │ 'for i length --' │ 0.10259999707341194 │
│ 12 │ 'do while ++' │ 0.12810000032186508 │
│ 13 │ '++ do while' │ 0.13450000062584877 │
│ 14 │ 'for ++' │ 0.13859999924898148 │
│ 15 │ 'while shift' │ 0.14709999784827232 │
│ 16 │ 'while ++' │ 0.1688999980688095 │
│ 17 │ '++ while' │ 0.21790000051259995 │
│ 18 │ '++ for' │ 0.235300000756979 │
│ 19 │ 'forEach call' │ 0.2394999973475933 │
│ 20 │ 'forEach' │ 0.24050000309944153 │
│ 21 │ 'map' │ 0.2498999983072281 │
│ 22 │ 'Object.keys map' │ 0.28459999710321426 │
│ 23 │ 'for of' │ 0.5731000006198883 │
│ 24 │ 'Array from' │ 0.8829999975860119 │
│ 25 │ 'for in' │ 2.3697999976575375 │
│ 26 │ 'flatMap' │ 3.9151000007987022 │
└─────────┴───────────────────────┴─────────────────────┘
arr with 100_000 elements
Best: pop: 0.034199997782707214ms
┌─────────┬───────────────────────┬──────────────────────┐
│ (index) │ type │ time │
├─────────┼───────────────────────┼──────────────────────┤
│ 0 │ 'pop' │ 0.034199997782707214 │
│ 1 │ 'do while forward --' │ 0.7470000013709068 │
│ 2 │ '--do while forward' │ 0.7613000012934208 │
│ 3 │ '--while forward' │ 0.8198000006377697 │
│ 4 │ '--for i length' │ 1.1184000000357628 │
│ 5 │ 'while shift' │ 1.1730000004172325 │
│ 6 │ '++ for' │ 1.3363999985158443 │
│ 7 │ 'for !== ++' │ 1.3478999994695187 │
│ 8 │ 'for i length --' │ 1.374600000679493 │
│ 9 │ 'do while ++' │ 1.3889999985694885 │
│ 10 │ '++ for !==' │ 1.405000001192093 │
│ 11 │ 'while ++' │ 1.4074000008404255 │
│ 12 │ '++ do while' │ 1.5060000009834766 │
│ 13 │ '-- for' │ 1.5149999968707561 │
│ 14 │ 'while forward --' │ 1.5166999995708466 │
│ 15 │ '--while >= 0' │ 1.5234000012278557 │
│ 16 │ 'forEach call' │ 1.6303000003099442 │
│ 17 │ 'for --' │ 1.6625000014901161 │
│ 18 │ 'forEach' │ 1.7166999988257885 │
│ 19 │ '++ while' │ 1.7875000014901161 │
│ 20 │ 'for ++' │ 1.7875999994575977 │
│ 21 │ 'map' │ 2.796399999409914 │
│ 22 │ 'for of' │ 3.0330999977886677 │
│ 23 │ 'for in' │ 11.203400000929832 │
│ 24 │ 'Array from' │ 12.01129999756813 │
│ 25 │ 'Object.keys map' │ 15.670400001108646 │
│ 26 │ 'flatMap' │ 24.128000002354383 │
└─────────┴───────────────────────┴──────────────────────┘
arr with 1_000_000 elements
Best: pop: 0.040699999779462814ms
┌─────────┬───────────────────────┬──────────────────────┐
│ (index) │ type │ time │
├─────────┼───────────────────────┼──────────────────────┤
│ 0 │ 'pop' │ 0.040699999779462814 │
│ 1 │ '--do while forward' │ 1.0359999984502792 │
│ 2 │ 'do while forward --' │ 1.0963000021874905 │
│ 3 │ '--while >= 0' │ 1.1332999989390373 │
│ 4 │ '++ do while' │ 1.2459000013768673 │
│ 5 │ '--for i length' │ 1.252700001001358 │
│ 6 │ '++ while' │ 1.2565000019967556 │
│ 7 │ 'do while ++' │ 1.3178000003099442 │
│ 8 │ 'while forward --' │ 1.333500001579523 │
│ 9 │ '++ for' │ 1.334900002926588 │
│ 10 │ 'while ++' │ 1.35529999807477 │
│ 11 │ '--while forward' │ 1.3923999965190887 │
│ 12 │ 'for i length --' │ 1.4184999987483025 │
│ 13 │ 'for !== ++' │ 1.5071000009775162 │
│ 14 │ '-- for' │ 1.5218000002205372 │
│ 15 │ 'for ++' │ 1.5582999996840954 │
│ 16 │ '++ for !==' │ 1.6731000021100044 │
│ 17 │ 'for of' │ 2.125 │
│ 18 │ 'for --' │ 2.4549999982118607 │
│ 19 │ 'forEach' │ 11.853199999779463 │
│ 20 │ 'forEach call' │ 12.711600001901388 │
│ 21 │ 'map' │ 17.96569999679923 │
│ 22 │ 'Array from' │ 59.12170000001788 │
│ 23 │ 'Object.keys map' │ 179.7318999990821 │
│ 24 │ 'for in' │ 213.46800000220537 │
│ 25 │ 'flatMap' │ 244.74289999902248 │
│ 26 │ 'while shift' │ 279.0199000015855 │
└─────────┴───────────────────────┴──────────────────────┘
arr with 10_000_000 elements
Best: pop: 0.007999997586011887ms
┌─────────┬───────────────────────┬──────────────────────┐
│ (index) │ type │ time │
├─────────┼───────────────────────┼──────────────────────┤
│ 0 │ 'pop' │ 0.007999997586011887 │
│ 1 │ 'for --' │ 3.2105999998748302 │
│ 2 │ '++ for' │ 3.2912000007927418 │
│ 3 │ '++ while' │ 3.328099999576807 │
│ 4 │ '-- for' │ 3.418000001460314 │
│ 5 │ 'for ++' │ 3.6288000009953976 │
│ 6 │ 'while ++' │ 3.69030000269413 │
│ 7 │ '++ for !==' │ 4.699599999934435 │
│ 8 │ 'for i length --' │ 6.014600001275539 │
│ 9 │ 'do while ++' │ 6.066599998623133 │
│ 10 │ '--for i length' │ 6.10190000012517 │
│ 11 │ 'for !== ++' │ 6.168099999427795 │
│ 12 │ 'while forward --' │ 6.1721000000834465 │
│ 13 │ '++ do while' │ 6.24439999833703 │
│ 14 │ 'do while forward --' │ 6.281200002878904 │
│ 15 │ '--while >= 0' │ 6.32039999961853 │
│ 16 │ '--while forward' │ 6.374599996954203 │
│ 17 │ '--do while forward' │ 6.550400000065565 │
│ 18 │ 'for of' │ 93.73619999736547 │
│ 19 │ 'forEach' │ 111.13800000026822 │
│ 20 │ 'forEach call' │ 112.34580000117421 │
│ 21 │ 'map' │ 175.25750000029802 │
│ 22 │ 'Array from' │ 532.1853000000119 │
│ 23 │ 'for in' │ 2311.785300001502 │
│ 24 │ 'flatMap' │ 2461.3560000024736 │
│ 25 │ 'Object.keys map' │ 3249.956099998206 │
│ 26 │ 'while shift' │ 7338.595100000501 │
└─────────┴───────────────────────┴──────────────────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment