Node.js perf_hooks Module
Node.js's perf_hooks The module is a performance monitoring tool that allows developers to measure and monitor the execution performance of code in their applications. It provides high-precision performance timing capabilities that can help you identify performance bottlenecks in your code.
perf_hooks is short for "Performance Hooks". It is implemented based on the Web Performance API standard and provides performance measurement capabilities for Node.js applications.
Core features of perf_hooks
1. Performance timing
perf_hooks The module can precisely measure the execution time of code blocks, with accuracy down to the nanosecond level.
2. Resource monitoring
It can monitor the resource usage of a Node.js process, including CPU time, memory usage, etc.
3. Performance observation
It provides an observer pattern that can continuously monitor specific types of performance metrics.
How to use perf_hooks
First, you need to import perf_hooks module:
const
{
performance
,
PerformanceObserver
}
=
require
(
'perf_hooks'
)
;
Basic performance measurement example
Example
const
{
performance
}
=
require
(
'perf_hooks'
)
;
// Record the start time
const
startTime
=
performance.
now
(
)
;
// Perform some operations
for
(
let i
=
0
;
i
<
1000000
;
i
++
)
{
Math
.
sqrt
(
i
)
;
}
// Record the end time
const
endTime
=
performance.
now
(
)
;
console.
log
(
`This code took
:
$
{
endTime
startTime
}
ms`
)
;
Using PerformanceObserver
PerformanceObserver You can listen for performance entries and respond to them:
Example
const
{
PerformanceObserver
,
performance
}
=
require
(
'perf_hooks'
)
;
const
obs
=
new
PerformanceObserver
(
(
items
)
=>
{
console.
log
(
items.
getEntries
(
)
[
0
]
.
duration
)
;
performance.
clearMarks
(
)
;
}
)
;
obs.
observe
(
{
entryTypes
:
[
'measure'
]
}
)
;
performance.
mark
(
'A'
)
;
doSomeLongOperation
(
)
;
performance.
mark
(
'B'
)
;
performance.
measure
(
'A to B'
,
'A'
,
'B'
)
;
The main APIs of perf_hooks
1. performance.now()
Returns the current high-precision timestamp, in milliseconds.
2. performance.mark(name)
Creates a named timestamp marker in the performance timeline.
3. performance.measure(name, startMark, endMark)
Measures the time interval between two markers.
4. PerformanceObserver
A class used to observe performance measurement events.
5. performance.timerify(fn)
Wraps a function so that its execution time can be measured.
Practical Application Scenarios
1. API response time monitoring
Example
const
{
performance
}
=
require
(
'perf_hooks'
)
;
app.
use
(
(
req
,
res
,
next
)
=>
{
const
start
=
performance.
now
(
)
;
res.
on
(
'finish'
,
(
)
=>
{
console.
log
(
`$
{
req.
method
}
$
{
req.
url
}
$
{
performance.
now
(
)
start
}
ms`
)
;
}
)
;
next
(
)
;
}
)
;
2. Function performance profiling
Example
const
{
performance
,
PerformanceObserver
}
=
require
(
'perf_hooks'
)
;
const
obs
=
new
PerformanceObserver
(
(
list
)
=>
{
const
entries
=
list.
getEntries
(
)
;
entries.
forEach
(
(
entry
)
=>
{
console.
log
(
`
Function
$
{
entry.
name
}
took $
{
entry.
duration
}
ms`
)
;
}
)
;
}
)
;
obs.
observe
(
{
entryTypes
:
[
'function'
]
}
)
;
function
slowFunction
(
)
{
// Some time-consuming operations
}
// Wrap the function with timerify
const
timedSlowFunction
=
performance.
timerify
(
slowFunction
)
;
timedSlowFunction
(
)
;
Notes
-
perf_hooksperformance measurement itself introduces some overhead, so it should be used cautiously in production environments. -
Timing measurement results can be affected by system load and other factors, so you should measure multiple times and take the average.
-
In different versions of Node.js,
perf_hooksthe implementation may differ, so you should pay attention to version compatibility. -
For very short operations (nanosecond level), measurement results may not be accurate enough.
Summary
Node.js's perf_hooks The module provides developers with powerful performance monitoring capabilities that can help identify and optimize performance bottlenecks in code. By using performance and PerformanceObserverreasonably, developers can obtain precise performance data and make more targeted optimization decisions.
In real projects, it is recommended to integrate performance monitoring with a logging system and establish long-term performance baselines so that performance regressions can be detected in a timely manner.
Node.js Built-in Modules