Skip to content

Instantly share code, notes, and snippets.

@malonehedges
Created November 28, 2017 16:28
Show Gist options
  • Save malonehedges/a105943fa3e721ddec2de7f8ee7b6f10 to your computer and use it in GitHub Desktop.
Save malonehedges/a105943fa3e721ddec2de7f8ee7b6f10 to your computer and use it in GitHub Desktop.
Timing of map, reduce, filter
import Game from '../Game'
import cards from './cards'
export default class CAH extends Game {
static getExpansionsPrettyButDumb () {
return cards
.map((card) => card.expansion)
.filter((expansion, index, arr) => arr.indexOf(expansion) === index)
}
static getExpansions () {
return cards.reduce((acc, card) => {
if (acc.indexOf(card.expansion) === -1) {
acc.push(card.expansion)
}
return acc
}, [])
}
}
import CAH from './games/CAH'
console.log('timing first one')
console.time('mapfilter')
for (let i = 0; i < 100; i++) {
CAH.getExpansionsPrettyButDumb()
}
console.timeEnd('mapfilter')
console.log('timing second one')
console.time('reduce')
for (let i = 0; i < 100; i++) {
CAH.getExpansions()
}
console.timeEnd('reduce')
// timing first one
// mapfilter: 1309.144ms
// timing second one
// reduce: 14.226ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment