Skip to content

Commit

Permalink
add comments for benches
Browse files Browse the repository at this point in the history
  • Loading branch information
retraigo committed Sep 18, 2022
1 parent 173e59c commit fe39301
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions benches/comparison.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/**
* Each module picks 1e4 items from the gacha pool.
* Total weight of pool: 21663
* Total number of items in pool: 905
*/

import { GachaMachine } from "../mod.ts";

import pokemon from "../testdata/pokemon.json" assert { type: "json" };

import {
RandomPicker,
pick,
pickMany,
flatten,
RandomPicker
} from "https://deno.land/x/wrand@v1.1.0/mod.ts";

import wrs from "https://esm.sh/weighted-randomly-select@1.0.6";
Expand All @@ -26,38 +29,56 @@ const items = pokemon.slice().map((x) => ({
const itemsForPicker = items.map(x => ({original: x.result, weight: x.chance}))
const itemsForRWC = items.map(x => ({id: x.result, weight: x.chance}))

console.log(items.reduce((acc, a) => acc + a.chance, 0));
console.log(items.reduce((acc, a) => acc + a.chance, 0), items.length);


/**
* No op
*/
Deno.bench("nop", () => {});

/**
* First one I could find on /x
*/
Deno.bench("masx200/weighted-randomly-select", () => {
for (let i = 0; i < 1e3; ++i) {
for (let i = 0; i < 1e4; ++i) {
select(items)
}
});



/**
* This was the one that inspired me to write fortuna iirc.
*/
Deno.bench("Rifdhan/weighted-randomly-select", () => {
for (let i = 0; i < 1e3; ++i) {
for (let i = 0; i < 1e4; ++i) {
wrs.select(items)
}
});


/**
* I don't really know how rwc works.
*/
Deno.bench("parmentf/random-weighted-choice", () => {
for (let i = 0; i < 1e3; ++i) {
rwc(items)
for (let i = 0; i < 1e4; ++i) {
rwc(itemsForRWC)
}
});

/**
* Initializing class is done in the bench too
* to avoid bias.
*/
Deno.bench("Balastrong/wrand", () => {
const picker = new RandomPicker(itemsForPicker);
picker.pickMany(1e3)
picker.pickMany(1e4)
});

/**
* Initializing class is done in the bench too
* to avoid bias.
*/
Deno.bench("retraigo/fortuna", () => {
const machine = new GachaMachine(items);
machine.get(1e3);
machine.get(1e4);
});

0 comments on commit fe39301

Please sign in to comment.