Created
April 15, 2017 15:07
-
-
Save kana-sama/5d5912de92a45eb162e106892a7eb6d4 to your computer and use it in GitHub Desktop.
array transformer merhods vs transducers
This file contains 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
const { Suite } = require("benchmark"); | |
const Random = require("random-js"); | |
const { map, into, compose, filter, take, pluck } = require("ramda"); | |
const suite = new Suite(); | |
const random = new Random(); | |
function present({ name, hz, count }) { | |
console.log( | |
` | |
Name: ${name} | |
HZ: ${hz} | |
Count: ${count} | |
` | |
); | |
} | |
function makeScore(gameID) { | |
const my = random.integer(0, 5); | |
const others = random.integer(0, 5); | |
return { gameID, my, others }; | |
} | |
function makeScores(length) { | |
return Array.from({ length }, (_, gameID) => makeScore(gameID)); | |
} | |
const scores = makeScores(1000); | |
suite | |
.add("Array methods", () => { | |
scores | |
.filter(({ my, others }) => my > others) | |
.map(({ gameID }) => gameID) | |
.slice(0, 2); | |
}) | |
.add("Transducers", () => { | |
into( | |
[], | |
compose( | |
filter(({ my, others }) => my > others), | |
pluck("gameID"), | |
take(2) | |
), | |
scores | |
); | |
}) | |
.on("complete", ({ currentTarget: { 0: array, 1: transducers } }) => { | |
present(array); | |
present(transducers); | |
}) | |
.run(); | |
/* | |
Name: Array methods | |
HZ: 4669.932543886201 | |
Count: 248 | |
Name: Transducers | |
HZ: 85011.15372556732 | |
Count: 4405 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment