0 day streak
javascript / intermediate
Snippet

Precise Timing with Performance Hooks

For intermediate profiling, use the Performance API. Unlike Date.now(), it provides high-resolution timestamps and structured measurements that can be consumed by monitoring tools.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
import { performance } from 'node:perf_hooks';
performance.mark('start-process');
// Heavy computation here
performance.mark('end-process');
performance.measure('ProcessDuration', 'start-process', 'end-process');
const [measure] = performance.getEntriesByName('ProcessDuration');
console.log(`Execution time: ${measure.duration.toFixed(2)}ms`);
nodejs
Breakdown
1
performance.mark(name)
Creates a timestamped marker in the performance timeline.
2
performance.measure(name, start, end)
Calculates the time difference between two named markers.