Node.js offers a rich set of performance data through its APIs, similar to how modern web browsers expose performance metrics for webpages.
With tools like the Performance Measurement API and the perf_hooks
module in
Node.js, you can gain insights into your application's performance from the
server's perspective, closely aligning with what the end user experiences.
In this article, the first of a two-part series about performance hooks, we'll explore how to approach performance optimization in Node.js. Along the way, we'll leverage Node's built-in timing APIs to enhance the efficiency of our application.
Let's get going!
Prerequisites
To follow along with this tutorial, you'll need a basic knowledge of Node.js and a recent version installed, preferably the latest LTS.
Understanding Fundamental Node.js Performance API Concepts
Before diving into Node.js performance APIs, it's important to understand some essential terms that frequently appear in discussions and documentation: a monotonic clock, high resolution time API, and performance timeline. Let's look at each of these in turn.
1. A Monotonic Clock
In JavaScript, the Date.now()
function generates timestamps based on the
system clock, counting from the Unix epoch (1970-01-01T00:00:00Z
). However,
using these timestamps for precise measurements can be unreliable because they
are vulnerable to any alterations in the system clock.
In contrast, a monotonic clock offers a timekeeping mechanism that always progresses forward, without any backward movement. Its key advantage lies in its stability against system clock changes, making it ideal for accurately measuring the time span between events or the duration of specific tasks.
2. High Resolution Time API
The High Resolution Time API provides a JavaScript interface for obtaining highly accurate time measurements. It uses a sub-millisecond monotonic timestamp that remains unaffected by any changes to the system clock.
This API's functionality is encapsulated in the performance.now()
method. It
returns a timestamp in sub-milliseconds, calculated relative to
performance.timeOrigin
— a UNIX timestamp denoting the initiation of a current
Node.js process.
Here's a quick example to illustrate its usage:
Running this code produces output such as:
This means that the console.log
statement runs about 26 milliseconds from the
initialization of the Node.js process.
Whenever you come across terms like "high resolution" or "high precision
timestamp" in JavaScript, this refers to values generated by the performance.now()
method.
3. Performance Timeline
The Performance Timeline in Node.js is a structured sequence of timing details and key performance milestones. These are captured both from the Node.js process itself and any code executed within your application.
This timeline consists of PerformanceEntry
objects (and subclasses), each
offering insights into the type and name of an event, along with precise
timestamps indicating both the start and the duration of the event.
Here's an example of such entries:
The Performance Timeline is mainly used to monitor and extract comprehensive performance metrics throughout the entire lifecycle of a Node.js application.
This information is invaluable for real-time performance analysis and can be seamlessly integrated with monitoring systems like AppSignal for Node.js to enhance application performance insights and diagnostics.
Inside the Node.js Performance API
Node.js offers a suite of Performance Measurement APIs within its perf_hooks
module that are specifically designed to record Node.js performance data.
These APIs are inspired by, and partially implement, the Web Performance APIs found in browser environments. However, they are tailored and adapted to suit server-side programming contexts.
In the following sections, we'll dive into some of the key APIs provided by this module:
PerformanceNodeTiming
Performance.now()
Performance.mark()
Performance.getEntries()
Performance.measure()
Performance.clearMarks()
Performance.clearMeasures()
PerformanceResourceTiming
1. PerformanceNodeTiming
In any Node.js program, the initial entry in the Performance Timeline is a
PerformanceNodeTiming
instance. It distinctively captures the timestamps of
significant milestones during the startup phase of a process. To access this
data, you can print the performance
object, as demonstrated below:
This results in an output like:
The nodeTiming
object, as part of the
Node.js runtime timings,
provides a detailed view of various stages in the process lifecycle. Here's a
breakdown of its key properties:
name
: Identifies the performance entry, which is the Node.js process in this case.entryType
: Specifies the type of performance entry.startTime
: Indicates when the measurement started, with 0 denoting the beginning of the process execution.duration
: Represents the duration from the start of the process to the point when the entry was recorded.nodeStart
: A high resolution timestamp marking the start of the Node.js runtime initialization.v8Start
: The time at which the V8 JavaScript engine began initializing.bootstrapComplete
: The time taken for the Node.js environment to complete setup and get ready to execute user scripts.environment
: The duration required to establish the Node.js environment.loopStart
: The high resolution timestamp marking the start of the event loop.loopExit
: Indicates when the event loop exited; a value of -1 suggests it was still active at the time of recording.idleTime
: The amount of time the event loop spent idle, not engaged in processing tasks.
2. Performance.now()
To measure an
operation's execution time in Node.js, you can use performance.now()
to generate high-resolution
timestamps.
By capturing two timestamps — one before the operation starts and another after its completion — and then calculating the difference between them, you can determine an operation's duration.
The following example demonstrates this:
Running this script typically results in an output like:
While this works for simple cases, there are more flexible methods available, which we'll explore later in this tutorial.
3. Performance.mark()
The performance.mark()
function is a powerful method for creating and adding
named markers to the Performance Timeline. These markers can later be retrieved
for performance analysis.
For instance, to track the start and end of an HTTP request, you might use it as follows:
This will result in an output similar to:
In this example, the startTime
for each PerformanceMark
object reflects a
high-resolution timestamp, akin to what performance.now()
provides. The
duration
for such objects is always zero since they represent specific moments
in time.
You can also enrich these markers with additional information using the detail
property:
Resulting in:
By adding the detail
property, the performance marks provide not just timing
information but also contextual data, enhancing the comprehensiveness of your
performance analysis.
4. Performance.getEntries()
A PerformanceEntry
is a unique entity representing a single performance metric
within an application's Performance Timeline. For instance, the
PerformanceMark
objects we discussed earlier are subclasses of
PerformanceEntry
, characterized by an entryType
of mark
.
Use getEntries()
to retrieve an array
of all the PerformanceEntry
objects logged up to a specific moment in
your program's execution. Here's an example:
Running this snippet of code produces an array output like this:
For situations where your Performance Timeline contains numerous entries and
you wish to access only specific subsets, Node.js offers getEntriesByName()
and getEntriesByType()
methods. These methods allow for filtering based on the
name
and entryType
properties of the performance entries:
These methods provide a more focused approach to examining performance data, enabling you to isolate and analyze specific aspects of your application's performance.
5. Performance.measure()
The Performance.measure()
method creates PerformanceMeasure
objects that
quantify the duration between two specific points in time (often marked
by PerformanceMark
objects) within an application's Performance Timeline:
If only the name
parameter is supplied, it will generate a
PerformanceMeasure
object capturing the program's duration up until the call:
This command results in an output like:
A more practical usage of measure()
is calculating the elapsed time between
two previously recorded PerformanceMark
objects:
This will output something similar to the following:
In this scenario, startTime
corresponds to the start time of the first mark
(mark_fetch_start
), and duration
is the difference in time between the two
marks.
This effectively measures the time that the HTTP request takes, offering a precise and practical way to assess specific durations within your application's performance timeline.
6. Performance.clearMarks()
The performance.clearMarks()
function removes PerformanceMark
objects from the Performance Timeline. This feature provides flexibility,
allowing you to either clear all existing marks or remove specific ones
identified by a name.
This is particularly useful for managing and resetting performance data, especially in long-running applications or during repeated testing phases.
The function can be employed as follows:
7. Performance.clearMeasures()
This function does the same thing as clearMarks()
but removes
PerformanceMeasure
objects instead.
8. PerformanceResourceTiming
The PerformanceResourceTiming
API is a specialized tool for capturing
detailed network timing data, allowing you to track the duration of network
requests in your code.
While we've used methods like performance.now()
,
performance.mark()
, and performance.measure()
to track the duration of an
HTTP request, they are more general-purpose tools.
In practice, the fetch
API automatically gathers performance timings for each
request, which are then recorded in the Performance Timeline as
PerformanceResourceTiming
objects.
Executing this script will result in an output resembling:
This PerformanceResourceTiming
object encapsulates
several measurements
for the HTTP request. Key attributes include:
startTime
,fetchStart
: The high resolution timestamp marking the commencement of the resource fetch.responseEnd
: The high resolution timestamp right after Node.js receives the last byte of the resource (or just before the transport connection closes, whichever occurs first).duration
: The difference betweenresponseEnd
andstartTime
.transferSize
: The total size of the fetched resource, including both response headers and body, measured in octets (commonly the same as bytes).
It's important to note that this data gets recorded in the timeline only when
the response is actually used. Omitting the await response.json()
line, for
instance, would result in an empty array.
Finally, let's take a quick look at how to manage resource buffer sizes.
Managing Resource Buffer Sizes in Node
The Performance Timeline has a default capacity limit for
PerformanceResourceTiming
objects, set at 250. To adjust this limit,
you can utilize the setResourceTimingBufferSize()
method:
This command increases the buffer size, allowing up to 500
PerformanceResourceTiming
objects to be stored simultaneously in the
Performance Timeline.
If you need to clear all the existing PerformanceResourceTiming
objects from
the timeline, the clearResourceTimings()
method comes in handy:
You can also listen for the resourcetimingbufferfull
event to take actions
when the performance resource timing buffer is full. For example, you could
increase the buffer capacity or clear the buffer so that new entries can be
added:
And that's it for now!
Up Next: Practical Uses of Node.js Performance APIs
In this blog post, we focused on examining the most important Node.js Performance APIs and their various uses.
In the next and final part of this series, we'll dive into practical implementations of these APIs. I'll demonstrate their real-world value to optimize and resolve performance issues in your Node.js applications.
Stay tuned, and happy coding!
P.S. If you liked this post, subscribe to our JavaScript Sorcery list for a monthly deep dive into more magical JavaScript tips and tricks.
P.P.S. If you need an APM for your Node.js app, go and check out the AppSignal APM for Node.js.