-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench.mjs
82 lines (74 loc) · 2.02 KB
/
bench.mjs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Benchmark from "benchmark";
import { render as svgj } from "./dist/index.mjs";
import { fstat, readdir, readdirSync, readFile as _readFile } from "fs";
import { promisify } from "util";
import * as path from "path";
import assert from "assert";
import { writeFile } from "fs/promises";
import svgr from "@svgr/core";
import chalk from "chalk";
const readFile = promisify(_readFile);
const readSVG = (input) => readFile(input, { encoding: "utf-8" });
let allFiles = [];
let tenFiles = [];
async function setup() {
const base = path.resolve(
path
.join(import.meta.url, "../", "svgs/dist/svg/bootstrap/")
.replace("file:", "")
);
const files = readdirSync(base);
for (let file of files) {
allFiles.push(
await readSVG(path.resolve("svgs/dist/svg/bootstrap/", file))
);
}
tenFiles = allFiles.slice(0, 10);
}
async function bench() {
await setup();
new Benchmark.Suite("svgj vs svgr (10 files)")
.add("svgj", () => {
for (let file of tenFiles) {
svgj(file);
}
})
.add("svgr sync", () => {
for (let file of tenFiles) {
svgr.default.sync(file);
}
})
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log(
"Fastest is " + chalk.green(this.filter("fastest").map("name"))
);
console.log(
"Slowest is " + chalk.red(this.filter("slowest").map("name"))
);
})
.run();
const oneFile = allFiles[20]; // arbitrary number
new Benchmark.Suite("svgj vs svgr (1 file)")
.add("svgj", () => {
svgj(oneFile);
})
.add("svgr sync", () => {
svgr.default.sync(oneFile);
})
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log(
"Fastest is " + chalk.green(this.filter("fastest").map("name"))
);
console.log(
"Slowest is " + chalk.red(this.filter("slowest").map("name"))
);
})
.run();
}
bench();