|
| 1 | +/** |
| 2 | + * Benchmark: getDummies / dataFrameGetDummies with prefix, dropFirst, dummyNa options. |
| 3 | + * Outputs JSON: {"function": "get_dummies_opts", "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 = 5; |
| 9 | +const ITERATIONS = 30; |
| 10 | + |
| 11 | +const categories = ["apple", "banana", "cherry", "date", "elderberry"]; |
| 12 | +const data = Array.from({ length: SIZE }, (_, i) => |
| 13 | + i % 20 === 0 ? null : categories[i % categories.length], |
| 14 | +); |
| 15 | +const s = new Series({ data }); |
| 16 | + |
| 17 | +const df = DataFrame.fromColumns({ |
| 18 | + fruit: Array.from({ length: SIZE }, (_, i) => |
| 19 | + i % 20 === 0 ? null : categories[i % categories.length], |
| 20 | + ), |
| 21 | + color: Array.from({ length: SIZE }, (_, i) => ["red", "green", "blue"][i % 3]), |
| 22 | +}); |
| 23 | + |
| 24 | +for (let i = 0; i < WARMUP; i++) { |
| 25 | + getDummies(s, { prefix: "cat", dummyNa: true }); |
| 26 | + getDummies(s, { dropFirst: true }); |
| 27 | + dataFrameGetDummies(df, { columns: ["fruit", "color"], prefix: "col", dropFirst: true }); |
| 28 | +} |
| 29 | + |
| 30 | +const start = performance.now(); |
| 31 | +for (let i = 0; i < ITERATIONS; i++) { |
| 32 | + getDummies(s, { prefix: "cat", dummyNa: true }); |
| 33 | + getDummies(s, { dropFirst: true }); |
| 34 | + dataFrameGetDummies(df, { columns: ["fruit", "color"], prefix: "col", dropFirst: true }); |
| 35 | +} |
| 36 | +const total = performance.now() - start; |
| 37 | + |
| 38 | +console.log(JSON.stringify({ function: "get_dummies_opts", mean_ms: total / ITERATIONS, iterations: ITERATIONS, total_ms: total })); |
0 commit comments