Skip to content

Instantly share code, notes, and snippets.

@kana-sama
Created April 15, 2017 15:07
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 kana-sama/5d5912de92a45eb162e106892a7eb6d4 to your computer and use it in GitHub Desktop.
Save kana-sama/5d5912de92a45eb162e106892a7eb6d4 to your computer and use it in GitHub Desktop.
array transformer merhods vs transducers
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