Skip to content

Instantly share code, notes, and snippets.

@tylerlong
Last active June 29, 2017 12:04
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 tylerlong/a64395b6b664882a3d81288de6e50abb to your computer and use it in GitHub Desktop.
Save tylerlong/a64395b6b664882a3d81288de6e50abb to your computer and use it in GitHub Desktop.
List filter & map performance comparision

Environment

  • Node v8.1.2
  • macOS 10.12.5
  • MacBook Pro (Retina, 15-inch, Mid 2015)

Speed (faster come first)

Ramda > Lodash > reduce > filter > for

Conclusion

  • It's good to learn funtional programming
    • for is the slowest.
    • The more functional, the faster
      • Ramda is faster than Lodash        - Note: this might not be ture, so I am just kidding. 😆
  • It's good to use a mature library
    • Ramda & Lodash are the fastest.
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`)
THIS IS A DATA SET OF: 10000000
======filter=====
THIS IS THE RESULT: 5000000
TIME SPENT: 895ms
THIS IS THE RESULT: 5000000
TIME SPENT: 1479ms
=====reduce======
THIS IS THE RESULT: 5000000
TIME SPENT: 358ms
THIS IS THE RESULT: 5000000
TIME SPENT: 362ms
=====Ramda=====
THIS IS THE RESULT: 5000000
TIME SPENT: 130ms
THIS IS THE RESULT: 5000000
TIME SPENT: 292ms
=====for=====
THIS IS THE RESULT: 5000000
TIME SPENT: 2254ms
THIS IS THE RESULT: 5000000
TIME SPENT: 2280ms
=====Lodash=====
THIS IS THE RESULT: 5000000
TIME SPENT: 235ms
THIS IS THE RESULT: 5000000
TIME SPENT: 275ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment