|
| 1 | +/** |
| 2 | + * Benchmark: Interval closed types — both, neither, left, right endpoint variants. |
| 3 | + * Tests closedLeft, closedRight, isOpen, isClosed, equals, and contains with all 4 closed types. |
| 4 | + * Outputs JSON: {"function": "interval_closed_types", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 5 | + */ |
| 6 | +import { Interval } from "../../src/index.ts"; |
| 7 | + |
| 8 | +const WARMUP = 5; |
| 9 | +const ITERATIONS = 100; |
| 10 | + |
| 11 | +const SIZE = 1_000; |
| 12 | +const closedTypes = ["both", "neither", "left", "right"] as const; |
| 13 | + |
| 14 | +const intervalSets = closedTypes.map((closed) => |
| 15 | + Array.from({ length: SIZE / 4 }, (_, i) => new Interval(i, i + 1, closed)), |
| 16 | +); |
| 17 | +const all = intervalSets.flat(); |
| 18 | +const ref = new Interval(0, 1, "right"); |
| 19 | + |
| 20 | +for (let w = 0; w < WARMUP; w++) { |
| 21 | + for (const iv of all.slice(0, 50)) { |
| 22 | + void iv.closedLeft; |
| 23 | + void iv.closedRight; |
| 24 | + void iv.isOpen; |
| 25 | + void iv.isClosed; |
| 26 | + void iv.mid; |
| 27 | + iv.contains(iv.mid); |
| 28 | + iv.equals(ref); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +const times: number[] = []; |
| 33 | +for (let i = 0; i < ITERATIONS; i++) { |
| 34 | + const t0 = performance.now(); |
| 35 | + for (const iv of all) { |
| 36 | + void iv.closedLeft; |
| 37 | + void iv.closedRight; |
| 38 | + void iv.isOpen; |
| 39 | + void iv.isClosed; |
| 40 | + void iv.mid; |
| 41 | + iv.contains(iv.mid); |
| 42 | + iv.equals(ref); |
| 43 | + } |
| 44 | + times.push(performance.now() - t0); |
| 45 | +} |
| 46 | + |
| 47 | +const total = times.reduce((a, b) => a + b, 0); |
| 48 | +console.log( |
| 49 | + JSON.stringify({ |
| 50 | + function: "interval_closed_types", |
| 51 | + mean_ms: round3(total / ITERATIONS), |
| 52 | + iterations: ITERATIONS, |
| 53 | + total_ms: round3(total), |
| 54 | + }), |
| 55 | +); |
| 56 | + |
| 57 | +function round3(v: number): number { |
| 58 | + return Math.round(v * 1000) / 1000; |
| 59 | +} |
0 commit comments