| name | profiling-benchmarking |
| description | Code profiling and benchmarking laws from 5 repos. Statistical benchmark harnesses, micro-second delta timing, string-processing performance comparison, IOPS throttle monitoring, and bundle size analysis. Sources: bestiejs/benchmark.js, alexhuszagh/rust-benchmarks, sindresorhus/time-span, holepunchto/hypercore, hughsk/disc. |
/profiling-benchmarking
When to Use
- Comparing two implementations before declaring one "faster"
- Measuring function execution time to microsecond precision
- Monitoring disk/network IOPS in a data pipeline
- Analyzing bundle size bloat after adding a dependency
- "This feels slow" — mandate measurement before any optimization
Do NOT use for
- One-shot scripts (profiling overhead exceeds the code itself)
- Claiming performance without evidence — always cite ops/sec or ms
The Golden Rule
Measure first. Optimize second. Measure again.
Never claim "faster" without benchmark.js ops/sec or process.hrtime() delta.
Status on any perf claim: REVIEWED (with numbers) or UNKNOWN (without).
Statistical Benchmark (benchmark.js)
import Benchmark from 'benchmark'
const suite = new Benchmark.Suite('string-ops')
suite
.add('regex replace', () => {
'hello world'.replace(/o/g, '0')
})
.add('split-join', () => {
'hello world'.split('o').join('0')
})
.on('cycle', e => console.log(String(e.target)))
.on('complete', function() {
console.log('Fastest: ' + this.filter('fastest').map('name'))
})
.run({ async: false })
Microsecond Delta Timing (process.hrtime / time-span)
import timeSpan from 'time-span'
const end = timeSpan()
await expensiveOperation()
const ms = end()
const ns = end.nanoseconds()
console.log(`Operation: ${ms.toFixed(3)}ms`)
const t0 = process.hrtime.bigint()
doWork()
const elapsed = Number(process.hrtime.bigint() - t0) / 1e6
String Processing Benchmark Pattern (rust-benchmarks principle)
const INPUTS = {
tiny: 'hello',
small: 'a'.repeat(100),
medium: 'a'.repeat(10_000),
large: 'a'.repeat(1_000_000),
}
for (const [label, input] of Object.entries(INPUTS)) {
new Benchmark.Suite(`split-at-comma [${label}]`)
.add('regex', () => input.split(/,/))
.add('indexOf', () => splitByIndexOf(input, ','))
.run()
}
IOPS Monitoring (hypercore pattern)
class IOPSMonitor {
#counts = { read: 0, write: 0 }
#bytes = { read: 0, write: 0 }
#start = Date.now()
recordRead(bytes) { this.#counts.read++; this.#bytes.read += bytes }
recordWrite(bytes) { this.#counts.write++; this.#bytes.write += bytes }
report() {
const elapsed = (Date.now() - this.#start) / 1000
return {
readIOPS: (this.#counts.read / elapsed).toFixed(1),
writeIOPS: (this.#counts.write / elapsed).toFixed(1),
readMBps: (this.#bytes.read / elapsed / 1e6).toFixed(2),
writeMBps: (this.#bytes. / elapsed / ).(),
}
}
}
Bundle Size Analysis (disc principle)
npx webpack --json > stats.json
npx webpack-bundle-analyzer stats.json
node --require ./perf-monitor.js app.js
Benchmark Checklist Before Claiming "Fast"
const perfReport = {
what: 'Replace regex vs split-join on 10k-char string',
before: '2,100,000 ops/sec ±1.2%',
after: '3,800,000 ops/sec ±0.9%',
gain: '+81%',
runs: 91,
node: process.version,
cpu: os.cpus()[0].model,
}
Anti-Fake-Pass Checklist
❌ "This is faster" claim without ops/sec number — status: UNKNOWN
❌ Benchmark with < 5 runs (statistically meaningless)
❌ Using Date.now() for sub-millisecond timing (1ms resolution)
❌ Benchmarking only the tiny input (cache effects hide large-input behavior)
❌ Bundle size analysis skipped after adding dependency > 20KB
❌ IOPS reported as raw count without elapsed time (not a rate)
❌ Perf test in the same process as other tests (JIT contamination)