Test name | Executions per second |
---|---|
for | 215210.2 Ops/sec |
map, filter, reduce | 93797.0 Ops/sec |
forEach | 192245.3 Ops/sec |
map | 129149.3 Ops/sec |
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let sum = 0;
for(let i = 0; i < arr.length; i++){
const item = arr[i] * 3;
if(item % 4 === 0) sum += item;
}
console.log(sum);
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = arr.map(item => item * 3)
.filter((item) => item % 4 === 0)
.reduce((item, total) => total = total + item);
console.log(sum);
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let sum = 0;
arr.forEach((item)=>{
item = item * 3;
if(item % 4 === 0) sum += item;
});
console.log(sum);
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let sum = 0;
arr.map((item)=>{
item = item * 3;
if(item % 4 === 0) sum += item;
});
console.log(sum);