javascript / expert
Snippet
Performance Profiling with timerify
The 'performance.timerify' function wraps a function to automatically measure its execution time and emit a performance entry. Combined with PerformanceObserver, this allows for non-intrusive monitoring of critical path performance without manually scattering timestamps in your logic.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
import { performance, PerformanceObserver } from 'node:perf_hooks';const obs = new PerformanceObserver((list) => {const entries = list.getEntries();entries.forEach(e => console.log(`${e.name}: ${e.duration.toFixed(4)}ms`));});obs.observe({ entryTypes: ['function'] });const heavyTask = () => { for(let i=0; i<1e6; i++); };const profiledTask = performance.timerify(heavyTask);profiledTask();
nodejs
Breakdown
1
obs.observe({ entryTypes: ['function'] });
Tells the observer to listen specifically for timerified function entries.
2
performance.timerify(heavyTask);
Returns a new function that triggers performance marks upon execution.