|
| 1 | +/** |
| 2 | + * Benchmark: pivotTable with multiple aggfuncs (sum, count, min, max) on 50k-row DataFrame. |
| 3 | + * Outputs JSON: {"function": "pivot_table_aggfunc_variants", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 4 | + */ |
| 5 | +import { DataFrame, pivotTable } from "../../src/index.ts"; |
| 6 | + |
| 7 | +const ROWS = 50_000; |
| 8 | +const WARMUP = 3; |
| 9 | +const ITERATIONS = 20; |
| 10 | + |
| 11 | +const regions = ["North", "South", "East", "West"]; |
| 12 | +const categories = ["A", "B", "C", "D", "E"]; |
| 13 | + |
| 14 | +const region = Array.from({ length: ROWS }, (_, i) => regions[i % regions.length] as string); |
| 15 | +const category = Array.from({ length: ROWS }, (_, i) => categories[i % categories.length] as string); |
| 16 | +const sales = Array.from({ length: ROWS }, (_, i) => (i % 1000) * 1.5 + 10); |
| 17 | + |
| 18 | +const df = DataFrame.fromColumns({ region, category, sales }); |
| 19 | + |
| 20 | +for (let i = 0; i < WARMUP; i++) { |
| 21 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "sum" }); |
| 22 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "count" }); |
| 23 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "min" }); |
| 24 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "max" }); |
| 25 | +} |
| 26 | + |
| 27 | +const start = performance.now(); |
| 28 | +for (let i = 0; i < ITERATIONS; i++) { |
| 29 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "sum" }); |
| 30 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "count" }); |
| 31 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "min" }); |
| 32 | + pivotTable(df, { values: "sales", index: "region", columns: "category", aggfunc: "max" }); |
| 33 | +} |
| 34 | +const total = performance.now() - start; |
| 35 | + |
| 36 | +console.log( |
| 37 | + JSON.stringify({ |
| 38 | + function: "pivot_table_aggfunc_variants", |
| 39 | + mean_ms: total / ITERATIONS, |
| 40 | + iterations: ITERATIONS, |
| 41 | + total_ms: total, |
| 42 | + }), |
| 43 | +); |
0 commit comments