Skip to content

Commit 8a3ac3d

Browse files
committed
feat: integrate tinybench sample
1 parent db83ee1 commit 8a3ac3d

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

packages/tinybench/benches/sample.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import Benchmark from "benchmark";
21
import { withCodSpeed } from "../";
2+
import { Bench } from "tinybench";
33

4-
const suite = withCodSpeed(new Benchmark.Suite());
4+
const bench = withCodSpeed(new Bench({ time: 100 }));
55

6-
suite
7-
.add("RegExp#test", function () {
8-
/o/.test("Hello World!");
6+
bench
7+
.add("switch 1", () => {
8+
let a = 1;
9+
let b = 2;
10+
const c = a;
11+
a = b;
12+
b = c;
913
})
10-
.add("String#indexOf", function () {
11-
"Hello World!".indexOf("o") > -1;
12-
})
13-
// add listeners
14-
.on("cycle", function (event: Benchmark.Event) {
15-
console.log(String(event.target));
16-
})
17-
// run async
18-
.run({ async: true });
14+
.add("switch 2", () => {
15+
let a = 1;
16+
let b = 10;
17+
a = b + a;
18+
b = a - b;
19+
a = b - a;
20+
});
21+
22+
await bench.run();
23+
24+
console.table(
25+
bench.tasks.map(({ name, result }) => ({
26+
"Task Name": name,
27+
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A",
28+
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A",
29+
}))
30+
);

0 commit comments

Comments
 (0)