Skip to content

Instantly share code, notes, and snippets.

@faiwer
Created April 16, 2017 06:37
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 faiwer/4c44386e0b3f0a139dd70057a8a8e681 to your computer and use it in GitHub Desktop.
Save faiwer/4c44386e0b3f0a139dd70057a8a8e681 to your computer and use it in GitHub Desktop.
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", () => {
const result = into(
[],
compose(
filter(({ my, others }) => my > others),
pluck("gameID"),
take(2)
),
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: { 0: array, 1: transducers, 2: foreach } }) => {
present(array);
present(transducers);
present(foreach);
})
.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment