forked from tsivinsky/hi-mom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
58 lines (48 loc) · 1.44 KB
/
bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Benchmark from "benchmark";
import names from "./names.js";
import { hiMom as originalHiMom } from "hi-mom";
import { hiMom as fasterHiMom } from "./index.js";
const suite = new Benchmark.Suite("hi-mom");
const length = 10_000;
const namesArr = new Array(length).fill(undefined).map(() => names[Math.floor(Math.random() * names.length)]);
const randomStrings = new Array(length).fill(undefined).map(() => {
return Math.floor(Math.random() * 100_000_000).toString(36);
});
let i = 0;
const opts = {
onStart() {
i = 0;
},
};
suite
.add("hi-mom no args", () => {
originalHiMom();
})
.add("faster-hi-mom no args", () => {
fasterHiMom();
})
.add("hi-mom with custom-name", () => {
originalHiMom("mama");
})
.add("faster-hi-mom with custom-name", () => {
fasterHiMom("mama");
})
.add(`hi-mom with ${length} names`, () => {
originalHiMom(namesArr[i++ % length]);
}, opts)
.add(`faster-hi-mom with ${length} names`, () => {
fasterHiMom(namesArr[i++ % length]);
}, opts)
.add(`hi-mom with ${length} random strings`, () => {
originalHiMom(randomStrings[i++ % length]);
}, opts)
.add(`faster-hi-mom with ${length} random strings`, () => {
fasterHiMom(randomStrings[i++ % length]);
}, opts)
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.run({ "async": true });