|
| 1 | +/** |
| 2 | + * Benchmark: toDictOriented / fromDictOriented — DataFrame ↔ dict conversions. |
| 3 | + * Tests all orient variants: "list", "records", "split", "index", "tight". |
| 4 | + * Outputs JSON: {"function": "to_from_dict", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 5 | + */ |
| 6 | +import { DataFrame, toDictOriented, fromDictOriented } from "../../src/index.js"; |
| 7 | + |
| 8 | +const SIZE = 10_000; |
| 9 | +const WARMUP = 5; |
| 10 | +const ITERATIONS = 50; |
| 11 | + |
| 12 | +const df = new DataFrame({ |
| 13 | + a: Array.from({ length: SIZE }, (_, i) => i), |
| 14 | + b: Array.from({ length: SIZE }, (_, i) => i * 1.5), |
| 15 | + c: Array.from({ length: SIZE }, (_, i) => `str_${i % 100}`), |
| 16 | +}); |
| 17 | + |
| 18 | +for (let i = 0; i < WARMUP; i++) { |
| 19 | + toDictOriented(df, "list"); |
| 20 | + toDictOriented(df, "records"); |
| 21 | + toDictOriented(df, "split"); |
| 22 | + toDictOriented(df, "index"); |
| 23 | + toDictOriented(df, "tight"); |
| 24 | + fromDictOriented({ a: [1, 2, 3], b: [4, 5, 6] }); |
| 25 | + fromDictOriented({ 0: { a: 1, b: 4 }, 1: { a: 2, b: 5 } }, "index"); |
| 26 | +} |
| 27 | + |
| 28 | +const start = performance.now(); |
| 29 | +for (let i = 0; i < ITERATIONS; i++) { |
| 30 | + toDictOriented(df, "list"); |
| 31 | + toDictOriented(df, "records"); |
| 32 | + toDictOriented(df, "split"); |
| 33 | + toDictOriented(df, "index"); |
| 34 | + toDictOriented(df, "tight"); |
| 35 | + fromDictOriented({ a: [1, 2, 3], b: [4, 5, 6] }); |
| 36 | + fromDictOriented({ 0: { a: 1, b: 4 }, 1: { a: 2, b: 5 } }, "index"); |
| 37 | +} |
| 38 | +const total = performance.now() - start; |
| 39 | + |
| 40 | +console.log( |
| 41 | + JSON.stringify({ |
| 42 | + function: "to_from_dict", |
| 43 | + mean_ms: Math.round((total / ITERATIONS) * 1000) / 1000, |
| 44 | + iterations: ITERATIONS, |
| 45 | + total_ms: Math.round(total * 1000) / 1000, |
| 46 | + }), |
| 47 | +); |
0 commit comments