|
| 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]); |
0 commit comments