|
const R = require('ramda') |
|
const _ = require('lodash') |
|
|
|
const getTimeInMillisenconds = () => { |
|
return (new Date).getTime(); |
|
} |
|
|
|
const list = R.range(0, 10000000) |
|
console.log(`THIS IS A DATA SET OF: ${list.length}`) |
|
|
|
console.log('======filter=====') |
|
|
|
let time = getTimeInMillisenconds() |
|
let result = list.filter(i => i % 2 === 0) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
time = getTimeInMillisenconds() |
|
result = list.filter(i => i % 2 === 0).map(i => i * 2) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
console.log() |
|
console.log('=====reduce======') |
|
|
|
time = getTimeInMillisenconds() |
|
result = list.reduce((acc, item) => { |
|
if (item % 2 === 0) acc.push(item) |
|
return acc |
|
}, []) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
time = getTimeInMillisenconds() |
|
result = list.reduce((acc, item) => { |
|
if (item % 2 === 0) acc.push(item * 2) |
|
return acc |
|
}, []) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
console.log() |
|
console.log('=====Ramda=====') |
|
|
|
time = getTimeInMillisenconds() |
|
result = R.filter(i => i % 2 === 0, list) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
time = getTimeInMillisenconds() |
|
result = R.map(i => i * 2, R.filter(i => i % 2 === 0, list)) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
|
|
console.log() |
|
console.log('=====for=====') |
|
|
|
time = getTimeInMillisenconds() |
|
result = [] |
|
for (const i in list) { |
|
if (i % 2 === 0) { |
|
result.push(i) |
|
} |
|
} |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
time = getTimeInMillisenconds() |
|
result = [] |
|
for (const i in list) { |
|
if (i % 2 === 0) { |
|
result.push(i * 2) |
|
} |
|
} |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
console.log() |
|
console.log('=====Lodash=====') |
|
|
|
time = getTimeInMillisenconds() |
|
result = _.filter(list, i => i % 2 === 0) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |
|
|
|
time = getTimeInMillisenconds() |
|
result = _.map(_.filter(list, i => i % 2 === 0), i => i * 2) |
|
console.log(`THIS IS THE RESULT: ${result.length}`) |
|
console.log(`TIME SPENT: ${getTimeInMillisenconds() - time}ms`) |