Created
April 16, 2017 06:37
-
-
Save faiwer/4c44386e0b3f0a139dd70057a8a8e681 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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