Skip to content

Instantly share code, notes, and snippets.

@kana-sama kana-sama/1.js
Last active Apr 16, 2017

Embed
What would you like to do?
/*
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
You can’t perform that action at this time.