Skip to content

Instantly share code, notes, and snippets.

@tnguven
Last active October 8, 2017 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnguven/36ce62f6f586bb4eb1af555e83b75080 to your computer and use it in GitHub Desktop.
Save tnguven/36ce62f6f586bb4eb1af555e83b75080 to your computer and use it in GitHub Desktop.
Filter vs Reduce
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