1const complexities = [
2 { name: 'O(1)', ops: (n) => 1 },
3 { name: 'O(log n)', ops: (n) => Math.ceil(Math.log2(n)) },
4 { name: 'O(n)', ops: (n) => n },
5 { name: 'O(n log n)', ops: (n) => Math.ceil(n * Math.log2(n)) },
6 { name: 'O(n²)', ops: (n) => n * n },
7 { name: 'O(2ⁿ)', ops: (n) => 2 ** n },
8];
9function countOps(name, n) {
10 const f = complexities.find((c) => c.name === name);
11 return f ? f.ops(n) : 0;
12}
13function estimate(ops, nsPerOp = 1) {
14 const t = ops * nsPerOp;
15 if (t < 1e3) return t + ' ns';
16 if (t < 1e6) return (t / 1e3).toFixed(1) + ' μs';
17 if (t < 1e9) return (t / 1e6).toFixed(1) + ' ms';
18 return (t / 1e9).toFixed(1) + ' s';
19}