Last active
October 8, 2017 20:27
-
-
Save tnguven/36ce62f6f586bb4eb1af555e83b75080 to your computer and use it in GitHub Desktop.
Filter vs Reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function letThemReduce(count) { | |
let bigData = []; | |
for (let i = 0; i < count; i++) { | |
bigData[i] = i; | |
} | |
console.log('Data count:', bigData.length); | |
// ///////////////////////////////////////////////////////////////////// FILTER MAP START HERE | |
console.time('map'); | |
let filterBigData = bigData.filter((item) => { | |
return item % 2 === 0; | |
}).map((item) => { | |
return item * 2; | |
}); | |
console.timeEnd('map'); | |
// ///////////////////////////////////////////////////////////////////// REDUCE START HERE | |
console.time('reduce'); | |
let reduceBigData = bigData.reduce((acc, item) => { | |
if (item % 2 === 0) { | |
acc.push(item * 2); | |
} | |
return acc; | |
}, []); | |
console.timeEnd('reduce'); | |
} | |
function runIt() { | |
for (i = 1; i < 4; i++) { | |
letThemReduce(9999 * (i * 111)); | |
} | |
} | |
runIt(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment