Skip to content

Instantly share code, notes, and snippets.

@kana-sama
Last active April 16, 2017 09:29
Show Gist options
  • Save kana-sama/d0d5eb8362dda32841cc58302fa1c191 to your computer and use it in GitHub Desktop.
Save kana-sama/d0d5eb8362dda32841cc58302fa1c191 to your computer and use it in GitHub Desktop.
/*
Name: Array methods
HZ: 4642.041341511592
Count: 240
Name: Transducer
HZ: 161841.1974395176
Count: 8669
Name: For
HZ: 786935.9631453991
Count: 40472
*/
const { Suite } = require("benchmark");
const Random = require("random-js");
const { compose, transduce, into, take, filter, 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);
function append(xs, x) {
xs.push(x);
return xs;
}
const transformation = compose(
filter(({ my, others }) => my > others),
pluck("gameID"),
take(2)
);
suite
.add("Array methods", () => {
scores
.filter(({ my, others }) => my > others)
.map(({ gameID }) => gameID)
.slice(0, 2);
})
.add("Transducer", () => {
transduce(transformation, append, [], scores);
})
.add("For", () => {
const result = [];
for (const { my, others, gameID } of scores)
if (my > others) {
result.push(gameID);
if (result.length >= 2) break;
}
})
.on("complete", ({ currentTarget }) => {
currentTarget.forEach(present);
})
.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment