Performance Basics: Measure First
Before making an API faster, measure it, with a timing middleware for one request and autocannon for many at once, because the usual causes of a slow route are CPU work that blocks the event loop, a query with no index, or a list with no paging, and only numbers show which one it is.
Freelancer · Node.js · Web Development · Updated
After this lesson you can
- Measure a route before changing it, with a timing middleware or autocannon's requests per second and latency
- Fix the usual causes of a slow route, such as CPU work that blocks the event loop, a query with no index, or a list with no paging
- Choose the tool that fits a production problem, from pino logs and a checked config to a health route, a graceful shutdown and autocannon
Key takeaways
- Guessing why an API is slow wastes days. Measure first, change one thing, then measure again.
- A timing middleware reads the time before a request and when its answer is sent, and pino-http's responseTime is the same number.
- autocannon sends many requests at once and reports latency, how long each answer took, and requests per second, how many it answered.
- Heavy CPU work in a route blocks the event loop, so every other request waits too. Do it once at startup, or move it to a worker thread.
- A query with no index reads every document, and a list with no paging sends everything, so add an index and a limit.
"The app is slow" is one of the most common bug reports you'll get, and one of the easiest to waste a week on. People guess: a faster computer, a cache, a rewrite. A few minutes of measuring usually shows the real cause, and shows whether your fix worked.
Riders say the Campus Shuttle's timetable for a stop takes a while to load. Here's the route:
app.get("/timetable/:stop", (req, res) => {
const rows = buildTimetable().filter((row) => row.stop === req.params.stop && row.day === 1);
res.json(rows);
});
buildTimetable() works out the whole semester: 120 days, three routes, every trip, every stop, 99,840 rows. Then the route keeps today's rows for one stop. Is that the slow part? Don't guess: measure.
One request: a timing middleware
A stopwatch for every request is a small middleware. It notes the time when the request arrives, and logs how long it took when the answer has been sent, which is when res fires its finish event:
app.use((req, res, next) => {
const start = performance.now();
res.on("finish", () => {
const ms = (performance.now() - start).toFixed(1);
console.log(`${req.method} ${req.originalUrl} ${res.statusCode} ${ms} ms`);
});
next();
});
Three requests for the library's timetable, then one for /health:
GET /timetable/library 200 16.9 ms
GET /timetable/library 200 12.7 ms
GET /timetable/library 200 11.0 ms
GET /health 200 0.5 ms
About 11 milliseconds each, which sounds fine. With pino-http, you already have this number: it's responseTime on every request's line. But one rider at a time isn't how an API is used.
Many at once: autocannon
Load testing means sending an API many requests at once, the way a busy morning would. autocannon (npm install --save-dev autocannon) does it from the terminal. This sends requests from 20 connections at once (-c 20) for 10 seconds (-d 10):
npx autocannon -c 20 -d 10 http://localhost:3100/timetable/library
Running 10s test @ http://localhost:3100/timetable/library
20 connections
┌─────────┬───────┬────────┬────────┬────────┬───────────┬──────────┬────────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼───────┼────────┼────────┼────────┼───────────┼──────────┼────────┤
│ Latency │ 17 ms │ 116 ms │ 169 ms │ 176 ms │ 113.25 ms │ 34.47 ms │ 217 ms │
└─────────┴───────┴────────┴────────┴────────┴───────────┴──────────┴────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬────────┬────────┬─────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼─────────┼─────────┼─────────┼─────────┼────────┼────────┼─────────┤
│ Req/Sec │ 154 │ 154 │ 171 │ 194 │ 175.4 │ 14.5 │ 154 │
├───────────┼─────────┼─────────┼─────────┼─────────┼────────┼────────┼─────────┤
│ Bytes/Sec │ 2.02 MB │ 2.02 MB │ 2.24 MB │ 2.54 MB │ 2.3 MB │ 190 kB │ 2.02 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴────────┴────────┴─────────┘
Req/Bytes counts sampled once per second.
# of samples: 10
2k requests in 10.05s, 23 MB read
Two rows matter. Latency (read "LAY-ten-see") is how long a rider waited for an answer: 113 milliseconds on average, and the 50% column says half the requests took 116 ms or less. Req/Sec is how many answers the API gave each second: about 175. These numbers come from one laptop; yours will differ, so compare your own before and after, never with someone else's.
CPU work blocks everyone
Eleven milliseconds alone became 113 under load, because Node runs your JavaScript on one thread, as The Event Loop, in Server Terms showed. While one request builds the timetable, every other request waits in line, even ones with nothing to do with timetables. Measuring /health with one connection for 5 seconds, while the timetable test runs, shows it:
/health, measured on one laptop |
Average latency | Requests per second |
|---|---|---|
| While the timetable test runs | 117.7 ms | 8.4 |
| With nothing else running | 0.01 ms | 17,032 |
Work that keeps the thread busy is blocking code. Here, the timetable doesn't change during the day, so the fix is to build it once, when the server starts, and keep each stop's rows ready in a Map:
const today = new Map();
for (const row of buildTimetable()) {
if (row.day !== 1) continue;
if (!today.has(row.stop)) today.set(row.stop, []);
today.get(row.stop).push(row);
}
app.get("/timetable/:stop", (req, res) => {
res.json(today.get(req.params.stop) ?? []);
});
The same autocannon test now reports an average latency of 1.15 ms and 11,838.91 requests per second, against 113.25 ms and 175.4 before, with the same answer. Heavy work that can't be done ahead goes to a worker thread, which Work That Runs After the Response shows.
Slow queries: an index, and a page at a time
The other usual cause is the database. With the semester's 99,840 arrivals in MongoDB, find({ stop: "library", day: 1 }) with explain("executionStats"), as in Indexes and Text Search, reports:
Found 208, looked at 99840 documents
After createIndex({ stop: 1, day: 1 }), the same query:
Found 208, looked at 208 documents
And a list that grows, such as every arrival a rider has asked about, gets paged with limit, as in Sorting and Paging Results, so one request never sends thousands of documents. Then measure again.
Check yourself
2 questions to try on the lesson page.
So what
Measure, change one thing, measure again: that habit, with a load test and the three usual causes in mind, fixes most "the app is slow" reports with numbers to prove it.