Skip to content

Instantly share code, notes, and snippets.

@kana-sama kana-sama/bench.js
Created Apr 15, 2017

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