|
| 1 | +/** |
| 2 | + * Benchmark: makeFloatFormatter + makePercentFormatter + makeCurrencyFormatter — formatter factories. |
| 3 | + * Outputs JSON: {"function": "formatter_factories_fn", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 4 | + */ |
| 5 | +import { |
| 6 | + Series, |
| 7 | + makeFloatFormatter, |
| 8 | + makePercentFormatter, |
| 9 | + makeCurrencyFormatter, |
| 10 | + applySeriesFormatter, |
| 11 | + applyDataFrameFormatter, |
| 12 | + DataFrame, |
| 13 | +} from "../../src/index.ts"; |
| 14 | + |
| 15 | +const SIZE = 10_000; |
| 16 | +const WARMUP = 5; |
| 17 | +const ITERATIONS = 50; |
| 18 | + |
| 19 | +const ser = new Series(Array.from({ length: SIZE }, (_, i) => i * 1.23456)); |
| 20 | +const df = DataFrame.fromColumns({ |
| 21 | + price: Array.from({ length: SIZE }, (_, i) => i * 9.99), |
| 22 | + pct: Array.from({ length: SIZE }, (_, i) => (i % 100) / 100), |
| 23 | +}); |
| 24 | + |
| 25 | +const fmtFloat = makeFloatFormatter(2); |
| 26 | +const fmtPct = makePercentFormatter(1); |
| 27 | +const fmtCur = makeCurrencyFormatter("$", 2); |
| 28 | + |
| 29 | +for (let i = 0; i < WARMUP; i++) { |
| 30 | + applySeriesFormatter(ser, fmtFloat); |
| 31 | + applyDataFrameFormatter(df, { price: fmtCur, pct: fmtPct }); |
| 32 | +} |
| 33 | + |
| 34 | +const start = performance.now(); |
| 35 | +for (let i = 0; i < ITERATIONS; i++) { |
| 36 | + makeFloatFormatter(3); |
| 37 | + makePercentFormatter(2); |
| 38 | + makeCurrencyFormatter("€", 2); |
| 39 | + applySeriesFormatter(ser, fmtFloat); |
| 40 | + applyDataFrameFormatter(df, { price: fmtCur, pct: fmtPct }); |
| 41 | +} |
| 42 | +const total = performance.now() - start; |
| 43 | + |
| 44 | +console.log( |
| 45 | + JSON.stringify({ |
| 46 | + function: "formatter_factories_fn", |
| 47 | + mean_ms: Math.round((total / ITERATIONS) * 1000) / 1000, |
| 48 | + iterations: ITERATIONS, |
| 49 | + total_ms: Math.round(total * 1000) / 1000, |
| 50 | + }), |
| 51 | +); |
0 commit comments