|
| 1 | +/** |
| 2 | + * Benchmark: CategoricalIndex modification — renameCategories, reorderCategories, removeCategories, |
| 3 | + * setCategories, removeUnusedCategories, asOrdered/asUnordered on a 10k-element index. |
| 4 | + * Outputs JSON: {"function": "categorical_index_modify", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 5 | + */ |
| 6 | +import { CategoricalIndex } from "../../src/index.ts"; |
| 7 | + |
| 8 | +const SIZE = 10_000; |
| 9 | +const WARMUP = 5; |
| 10 | +const ITERATIONS = 50; |
| 11 | + |
| 12 | +const CATS = ["alpha", "beta", "gamma", "delta", "epsilon"]; |
| 13 | +const labels = Array.from({ length: SIZE }, (_, i) => CATS[i % CATS.length]); |
| 14 | +const ci = CategoricalIndex.fromArray(labels); |
| 15 | + |
| 16 | +for (let i = 0; i < WARMUP; i++) { |
| 17 | + ci.renameCategories(["A", "B", "C", "D", "E"]); |
| 18 | + ci.reorderCategories(["epsilon", "delta", "gamma", "beta", "alpha"]); |
| 19 | + ci.removeCategories(["epsilon"]); |
| 20 | + ci.setCategories(["alpha", "beta", "gamma"]); |
| 21 | + ci.removeUnusedCategories(); |
| 22 | + ci.asOrdered(); |
| 23 | + ci.asUnordered(); |
| 24 | +} |
| 25 | + |
| 26 | +const times: number[] = []; |
| 27 | +for (let i = 0; i < ITERATIONS; i++) { |
| 28 | + const t0 = performance.now(); |
| 29 | + ci.renameCategories(["A", "B", "C", "D", "E"]); |
| 30 | + ci.reorderCategories(["epsilon", "delta", "gamma", "beta", "alpha"]); |
| 31 | + ci.removeCategories(["epsilon"]); |
| 32 | + ci.setCategories(["alpha", "beta", "gamma"]); |
| 33 | + ci.removeUnusedCategories(); |
| 34 | + ci.asOrdered(); |
| 35 | + ci.asUnordered(); |
| 36 | + times.push(performance.now() - t0); |
| 37 | +} |
| 38 | + |
| 39 | +const total = times.reduce((a, b) => a + b, 0); |
| 40 | +console.log( |
| 41 | + JSON.stringify({ |
| 42 | + function: "categorical_index_modify", |
| 43 | + mean_ms: total / ITERATIONS, |
| 44 | + iterations: ITERATIONS, |
| 45 | + total_ms: total, |
| 46 | + }), |
| 47 | +); |
0 commit comments