|
| 1 | +/** |
| 2 | + * Benchmark: readCsv with options — sep, header, skipRows, dtype casting. |
| 3 | + * Outputs JSON: {"function": "read_csv_options", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 4 | + */ |
| 5 | +import { readCsv } from "../../src/index.ts"; |
| 6 | + |
| 7 | +const ROWS = 10_000; |
| 8 | +const WARMUP = 3; |
| 9 | +const ITERATIONS = 20; |
| 10 | + |
| 11 | +// Build pipe-separated CSV (no header) |
| 12 | +const pipeLines: string[] = []; |
| 13 | +for (let i = 0; i < ROWS; i++) { |
| 14 | + pipeLines.push(`${i}|${(i * 1.1).toFixed(4)}|cat_${i % 50}`); |
| 15 | +} |
| 16 | +const pipeCsv = pipeLines.join("\n"); |
| 17 | + |
| 18 | +// Build comma-separated CSV (skip first 2 rows) |
| 19 | +const skipLines: string[] = ["# comment row 1", "# comment row 2", "id,value,label"]; |
| 20 | +for (let i = 0; i < ROWS; i++) { |
| 21 | + skipLines.push(`${i},${(i * 2.2).toFixed(4)},grp_${i % 20}`); |
| 22 | +} |
| 23 | +const skipCsv = skipLines.join("\n"); |
| 24 | + |
| 25 | +// Build CSV for dtype override |
| 26 | +const dtypeLines: string[] = ["id,value,flag"]; |
| 27 | +for (let i = 0; i < ROWS; i++) { |
| 28 | + dtypeLines.push(`${i},${i * 1.5},${i % 2}`); |
| 29 | +} |
| 30 | +const dtypeCsv = dtypeLines.join("\n"); |
| 31 | + |
| 32 | +for (let i = 0; i < WARMUP; i++) { |
| 33 | + readCsv(pipeCsv, { sep: "|", header: null }); |
| 34 | + readCsv(skipCsv, { skipRows: 2 }); |
| 35 | + readCsv(dtypeCsv, { dtype: { id: "int32", value: "float32" } }); |
| 36 | +} |
| 37 | + |
| 38 | +const times: number[] = []; |
| 39 | +for (let i = 0; i < ITERATIONS; i++) { |
| 40 | + const t0 = performance.now(); |
| 41 | + readCsv(pipeCsv, { sep: "|", header: null }); |
| 42 | + readCsv(skipCsv, { skipRows: 2 }); |
| 43 | + readCsv(dtypeCsv, { dtype: { id: "int32", value: "float32" } }); |
| 44 | + times.push(performance.now() - t0); |
| 45 | +} |
| 46 | + |
| 47 | +const totalMs = times.reduce((a, b) => a + b, 0); |
| 48 | +const meanMs = totalMs / ITERATIONS; |
| 49 | +console.log( |
| 50 | + JSON.stringify({ |
| 51 | + function: "read_csv_options", |
| 52 | + mean_ms: meanMs, |
| 53 | + iterations: ITERATIONS, |
| 54 | + total_ms: totalMs, |
| 55 | + }), |
| 56 | +); |
0 commit comments