Skip to content

Commit ec80e22

Browse files
committed
chore(benchmark): add micro-benchmark runner
1 parent cae1204 commit ec80e22

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

benchmark/index.mjs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { performance } from "node:perf_hooks";
2+
import * as corekit from "../dist/bundle/kr-corekit.js";
3+
4+
const {
5+
chunk,
6+
flattenDeep,
7+
get,
8+
merge,
9+
camelCase,
10+
isEqual,
11+
map,
12+
mean,
13+
mapAsync,
14+
} = corekit;
15+
16+
function run(name, fn, iterations = 20000) {
17+
fn();
18+
const startedAt = performance.now();
19+
20+
for (let index = 0; index < iterations; index++) {
21+
fn();
22+
}
23+
24+
const elapsed = performance.now() - startedAt;
25+
const opsPerSec = Math.round((iterations / elapsed) * 1000);
26+
27+
return { name, iterations, elapsedMs: elapsed.toFixed(2), opsPerSec };
28+
}
29+
30+
async function runAsync(name, fn, iterations = 2000) {
31+
await fn();
32+
const startedAt = performance.now();
33+
34+
for (let index = 0; index < iterations; index++) {
35+
await fn();
36+
}
37+
38+
const elapsed = performance.now() - startedAt;
39+
const opsPerSec = Math.round((iterations / elapsed) * 1000);
40+
41+
return { name, iterations, elapsedMs: elapsed.toFixed(2), opsPerSec };
42+
}
43+
44+
const sampleArray = Array.from({ length: 1000 }, (_, index) => index);
45+
const nestedArray = [1, [2, [3, [4, [5, [6]]]]]];
46+
const deepObject = { user: { profile: { address: { city: "Seoul" } } } };
47+
const mergeTarget = { a: { b: 1 }, c: [1, 2, 3] };
48+
const mergeSource = { a: { d: 2 }, c: [3, 4], e: true };
49+
50+
const syncResults = [
51+
run("array.chunk", () => chunk(sampleArray, 25)),
52+
run("array.flattenDeep", () => flattenDeep(nestedArray)),
53+
run("object.get", () => get(deepObject, "user.profile.address.city")),
54+
run("object.merge", () => merge({ ...mergeTarget }, mergeSource), 5000),
55+
run("string.camelCase", () => camelCase("kr corekit benchmark sample")),
56+
run("lang.isEqual", () => isEqual({ x: [1, 2] }, { x: [1, 2] }), 10000),
57+
run("collection.map", () => map({ a: 1, b: 2, c: 3 }, (v) => v * 2)),
58+
run("math.mean", () => mean([1, 2, 3, 4, 5])),
59+
];
60+
61+
const asyncResults = [
62+
await runAsync(
63+
"async.mapAsync",
64+
() => mapAsync([1, 2, 3, 4], async (value) => value * 2),
65+
1000
66+
),
67+
];
68+
69+
console.log("Benchmark Results");
70+
console.table([...syncResults, ...asyncResults]);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
},
124124
"scripts": {
125125
"build": "tsc && vite build",
126+
"benchmark": "npm run build && node benchmark/index.mjs",
126127
"build:analyze": "npm run build && npm run analyze:bundle",
127128
"analyze:bundle": "echo 'Bundle Analysis:' && du -h dist/bundle/* && echo '\\nGzipped sizes:' && gzip -c dist/bundle/kr-corekit.js | wc -c | awk '{print \"ESM: \" $1/1024 \" KB\"}' && gzip -c dist/bundle/kr-corekit.cjs | wc -c | awk '{print \"CJS: \" $1/1024 \" KB\"}'",
128129
"pack:preview": "npm pack --dry-run",

0 commit comments

Comments
 (0)