-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.bench.ts
68 lines (60 loc) · 1.23 KB
/
mod.bench.ts
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
59
60
61
62
63
64
65
66
67
68
import { bench, runBenchmarks } from "https://deno.land/std/testing/bench.ts";
import Iter from "./mod.ts";
import { parseIntegral, range } from "./util.ts";
const rand = (v: number) => Math.random() * v % 100 | 0;
const RUNS = parseIntegral(Deno.args[0]) ?? (() => {
throw new Error("Run count unspecified or invalid");
})();
const ONLY = new RegExp(Deno.args[1]);
const COUNT = 1000000;
bench({
name: "max with Iter",
runs: RUNS,
func(b) {
b.start();
const _v = new Iter(range(COUNT)).map(rand).max();
b.stop();
void _v;
},
});
bench({
name: "max with vanilla JS array methods",
runs: RUNS,
func(b) {
b.start();
const _v = [...range(COUNT)].map(rand).reduce((max, v) => {
if (v > max) {
return v;
}
return max;
});
b.stop();
void _v;
},
});
bench({
name: "max with for loop",
runs: RUNS,
func(b) {
b.start();
let max = 0;
for (let elem of range(COUNT)) {
elem = rand(elem);
if (elem > max) {
max = elem;
}
}
b.stop();
void max;
},
});
bench({
name: "enumerate",
runs: RUNS,
func(b) {
b.start();
for (const _ of new Iter(range(COUNT)).enumerate());
b.stop();
},
});
runBenchmarks({ only: ONLY });