|
| 1 | +/** |
| 2 | + * Benchmark: getDummies / dataFrameGetDummies — one-hot encoding. |
| 3 | + * Outputs JSON: {"function": "get_dummies", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 4 | + */ |
| 5 | +import { getDummies, dataFrameGetDummies, Series, DataFrame } from "../../src/index.ts"; |
| 6 | + |
| 7 | +const SIZE = 10_000; |
| 8 | +const WARMUP = 3; |
| 9 | +const ITERATIONS = 30; |
| 10 | + |
| 11 | +const categories = ["A", "B", "C", "D", "E"]; |
| 12 | +const s = new Series({ data: Array.from({ length: SIZE }, (_, i) => categories[i % categories.length]) }); |
| 13 | +const df = new DataFrame({ |
| 14 | + cat1: Array.from({ length: SIZE }, (_, i) => categories[i % categories.length]), |
| 15 | + cat2: Array.from({ length: SIZE }, (_, i) => ["x", "y", "z"][i % 3]), |
| 16 | +}); |
| 17 | + |
| 18 | +for (let i = 0; i < WARMUP; i++) { |
| 19 | + getDummies(s); |
| 20 | + dataFrameGetDummies(df, { columns: ["cat1", "cat2"] }); |
| 21 | +} |
| 22 | + |
| 23 | +const times: number[] = []; |
| 24 | +for (let i = 0; i < ITERATIONS; i++) { |
| 25 | + const start = performance.now(); |
| 26 | + getDummies(s); |
| 27 | + dataFrameGetDummies(df, { columns: ["cat1", "cat2"] }); |
| 28 | + times.push(performance.now() - start); |
| 29 | +} |
| 30 | + |
| 31 | +const totalMs = times.reduce((a, b) => a + b, 0); |
| 32 | +const meanMs = totalMs / ITERATIONS; |
| 33 | +console.log( |
| 34 | + JSON.stringify({ |
| 35 | + function: "get_dummies", |
| 36 | + mean_ms: Math.round(meanMs * 1000) / 1000, |
| 37 | + iterations: ITERATIONS, |
| 38 | + total_ms: Math.round(totalMs * 1000) / 1000, |
| 39 | + }), |
| 40 | +); |
0 commit comments