Node.js performance: measure first, then fix the slow spot

Improve Node.js performance by measuring: read logs for the slow request, use --cpu-prof to find the costly function, fix it with a Map and verify the result.

To improve Node.js performance, measure before you fix: use logs to learn which request is slow, then use a profiler to learn which function takes the time. Guessing the slow spot is usually wrong. Have you ever fixed the wrong thing because "it must be that"? Let's go through one example.

The log says which request is slow, but not where

In the previous post, the order server of the Mộc café got JSON logs: every request has one summary line with the status code and the number of milliseconds it took. (Mộc is an illustrative café that I use as the running example in this series.) I ended that post with a question: when a request is slow, the log only tells you which request is slow, not which line of code is slow.

Now the owner asks for a report page: how many orders each customer placed and how much they spent in total, with the biggest spender first. I add a GET /report path to the server of the previous post, with a function of four lines:

Added to src/order-server.ts of the previous post
// In the route function: if (request.url === "/report") return sendReport(response, orders);

// A report of the total spending by customer, for the admin page of the owner.
function sendReport(response: ServerResponse, orders: Order[]): void {
  response.writeHead(200, JSON_HEADERS).end(JSON.stringify(reportByCustomer(orders)));
}

I tried it with 50,000 orders in memory (large enough to see the problem, and a number I picked myself, not the real number of orders of a café), called /orders and then /report, and these are the two summary log lines:

Log of the two requests (the fields method, requestId and time are dropped)
{"level":"info","msg":"request","path":"/orders","status":200,"ms":30}
{"level":"info","msg":"request","path":"/report","status":200,"ms":1984}

With the same number of orders, one path takes 30 ms and the other takes almost two seconds. The log did its job: it points at /report. Where exactly it is slow is the part we still have to find.

I guessed first, and guessed wrong

The most natural approach is to guess. I immediately thought of two suspects: formatting the money with toLocaleString, because it sounds heavy, and JSON.stringify of an array with almost 18,000 rows. Before I fix anything, I measure those two on their own. So that you can follow along, I rebuild the necessary parts in a new folder, no server needed:

Folder layout
report/
  package.json
  src/orders.ts
  src/make-data.ts
  src/report.ts           (the first version, in this section)
  measure.ts
  guess.ts
  read-profile.ts
  compare-runs.ts
package.json
{
  "name": "report",
  "private": true,
  "type": "module",
  "devDependencies": { "vitest": "5.1.0" }
}

You need Node.js 22.18 or newer, because from that version Node runs .ts files directly. src/orders.ts only needs the order type and a function that computes the total of one order:

src/orders.ts
export type Customer = { name: string; phone: string };
export type OrderItem = { name: string; price: number; quantity: number };
export type Order = {
  id: number;
  createdAt: string;
  source: "web" | "legacy-pos";
  customer?: Customer; // a counter sale may have no customer
  items: OrderItem[];
};

export function orderTotal(order: Order): number {
  return order.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}

To get large data that is the same on every run (otherwise your numbers and mine cannot be compared), src/make-data.ts generates fake orders with a random number generator that has a fixed seed. About 10% of the orders are counter sales with no customer information. Prices are in Vietnamese đồng, so 25,000 is about one US dollar:

src/make-data.ts
// Generate a big list of orders that is the same on every run (a random number generator with a fixed seed).
import type { Order } from "./orders.ts";

// mulberry32: a tiny random number generator; the same seed gives the same sequence.
function randomFromSeed(seed: number): () => number {
  let a = seed;
  return () => {
    a = (a + 0x6d2b79f5) | 0;
    let t = Math.imul(a ^ (a >>> 15), 1 | a);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

const MENU: [string, number][] = [["black coffee", 25000], ["milk coffee", 29000], ["bac xiu", 32000], ["salt coffee", 35000], ["egg banh mi", 25000]];

export function makeOrders(orderCount: number, customerCount: number, seed = 42): Order[] {
  const random = randomFromSeed(seed);
  return Array.from({ length: orderCount }, (_, i) => {
    const hasCustomer = random() < 0.9; // 10% are counter sales with no customer information
    const index = Math.floor(random() * customerCount);
    const itemCount = 1 + Math.floor(random() * 3);
    return {
      id: 10_000 + i,
      createdAt: new Date(Date.UTC(2026, 0, 1) + i * 600_000).toISOString(),
      source: hasCustomer ? "web" : "legacy-pos",
      ...(hasCustomer ? { customer: { name: `Customer ${index}`, phone: `09${String(10_000_000 + index).padStart(8, "0")}` } } : {}),
      items: Array.from({ length: itemCount }, () => {
        const [name, price] = MENU[Math.floor(random() * MENU.length)];
        return { name, price, quantity: 1 + Math.floor(random() * 2) };
      }),
    } satisfies Order;
  });
}

Here is the first version of the report function. I wrote it to be easy to read, and at the scale of a few hundred orders it is perfectly fine: for every order, look for whether that customer already has a row in the result, add to it if so, and add a new row if not:

src/report.ts
import { orderTotal, type Order } from "./orders.ts";

export type ReportRow = { customer: string; orderCount: number; total: number };

// A report for the owner: how many orders each customer placed and how much they spent in total, the biggest spender first.
// THE FIRST VERSION, written to be easy to read: for each order, look for the row of that customer in the result.
export function reportByCustomer(orders: Order[]): ReportRow[] {
  const result: ReportRow[] = [];
  for (const order of orders) {
    const customer = order.customer?.phone ?? "walk-in";
    const row = result.find((r) => r.customer === customer);
    if (row) {
      row.orderCount += 1;
      row.total += orderTotal(order);
    } else {
      result.push({ customer, orderCount: 1, total: orderTotal(order) });
    }
  }
  return result.sort((a, b) => b.total - a.total);
}

Now two measuring scripts. The first measures the first version with 50,000 orders from 20,000 customers, and the second measures the two suspects on their own:

measure.ts
// Measure the time of the first version of reportByCustomer with 50,000 orders. Run: node measure.ts
import { reportByCustomer } from "./src/report.ts";
import { makeOrders } from "./src/make-data.ts";

const orders = makeOrders(50_000, 20_000);
const start = performance.now();
const report = reportByCustomer(orders);
console.log(`${orders.length} orders → ${report.length} rows, took ${Math.round(performance.now() - start)} ms`);
console.log("Top of the table:", JSON.stringify(report[0]));
guess.ts
// The first hypothesis: "it must be the money formatting and the JSON conversion". Measure those two parts on their own. Run: node guess.ts
import { reportByCustomer } from "./src/report.ts";
import { makeOrders } from "./src/make-data.ts";

const report = reportByCustomer(makeOrders(50_000, 20_000));

let start = performance.now();
const formattedMoney = report.map((row) => `${row.total.toLocaleString("en-US")} VND`);
console.log(`Formatting the money for ${formattedMoney.length} rows: ${Math.round(performance.now() - start)} ms`);

start = performance.now();
const json = JSON.stringify(report);
console.log(`JSON.stringify (${json.length} characters): ${Math.round(performance.now() - start)} ms`);

Run the two scripts:

Measure the first version and the two suspects
node measure.ts
node guess.ts
Output (the numbers on your machine will differ)
50000 orders → 17890 rows, took 2021 ms
Top of the table: {"customer":"walk-in","orderCount":4924,"total":428645000}
Formatting the money for 17890 rows: 21 ms
JSON.stringify (998342 characters): 7 ms

The two suspects add up to only about 28 ms out of almost 2,000 ms. If I had "optimized" the money formatting, for example by writing my own function that inserts the separators, I would have lost an afternoon, added code that has to be maintained, and the report would still take almost two seconds. That is why I like to measure first.

What does the profiler say takes the time?

When you can no longer guess, you use a profiler: a tool that records, many times per millisecond, which function the program is running. Node.js has a built-in flag --cpu-prof that writes a .cpuprofile file when the program ends, and --cpu-prof-dir chooses the folder to write to. You can read about these flags in the command line documentation of Node.js. The file can be opened with tools in the browser (Chrome DevTools), but I like to read it myself with a small script, because that way you understand what it contains: a list of samples, each recording which function was running, plus the time between samples.

read-profile.ts
// Read the .cpuprofile file written by `node --cpu-prof` and print the functions that take the most time (their "self" time: child functions not counted).
// Run: node read-profile.ts <file.cpuprofile>
import { readFileSync } from "node:fs";

type Node = { id: number; callFrame: { functionName: string; url: string; lineNumber: number } };
type Profile = { nodes: Node[]; samples: number[]; timeDeltas: number[] };

const file = process.argv[2];
if (!file) throw new Error("Usage: node read-profile.ts <file.cpuprofile>");
const profile: Profile = JSON.parse(readFileSync(file, "utf8"));

const nodeById = new Map(profile.nodes.map((node) => [node.id, node]));
const microsecondsByFunction = new Map<string, number>();
// Sample i lasts timeDeltas[i + 1] microseconds (until the next sample).
profile.samples.forEach((nodeId, i) => {
  const { functionName, url, lineNumber } = nodeById.get(nodeId)!.callFrame;
  const name = `${functionName || "(anonymous)"} ${url.split("/").slice(-2).join("/")}:${lineNumber + 1}`;
  microsecondsByFunction.set(name, (microsecondsByFunction.get(name) ?? 0) + (profile.timeDeltas[i + 1] ?? 0));
});

const total = [...microsecondsByFunction.values()].reduce((a, b) => a + b, 0);
const top = [...microsecondsByFunction].sort((a, b) => b[1] - a[1]).slice(0, 5);
for (const [name, microseconds] of top) console.log(`${String(Math.round(microseconds / 1000)).padStart(6)} ms  ${((microseconds / total) * 100).toFixed(1).padStart(5)}%  ${name}`);

I ran the first version through the profiler. The flag --no-turbo-inlining is a flag of V8 (the engine that runs JavaScript in Node), which turns off merging small functions into the place that calls them. When a function is merged, its time is counted in the caller, and the result is harder to read. Turning it off makes the program a little slower while measuring, but shows more clearly which function is which:

Record a profile, then read it
node --no-turbo-inlining --cpu-prof --cpu-prof-dir=prof measure.ts
node read-profile.ts prof/*.cpuprofile
Output
  1738 ms   49.9%  (anonymous) src/report.ts:10
  1282 ms   36.8%  reportByCustomer src/report.ts:6
   266 ms    7.6%  (anonymous) perf/measure.ts:1
    98 ms    2.8%  (anonymous) src/make-data.ts:19
    24 ms    0.7%  (garbage collector) :0

The first column is the self time of the function (child functions not counted), and the second is the percentage of the whole program. Line 10 of src/report.ts is the function (r) => r.customer === customer, the comparison function inside find. Line 6 is reportByCustomer itself, where the inner loop of find runs (because find is a built-in function of V8, its time is counted for the place that calls it). The first two lines together are almost 87% of the time, and both belong to one job: looking for a customer in a list. The data generation (the fourth line) takes only 2.8%, and the folder name perf in the third line is the name of the folder I ran in, yours will differ.

Why does one find cost so much? Because find goes from the start of the list and compares row by row until it meets the customer it needs. With 20,000 different customers, the result list grows to nearly 18,000 rows, and every order has to scan on average half of that list, and in the worst case the whole list when the customer is not there yet. The total number of comparisons is in the hundreds of millions. In the language of the trade: the time grows with the square of the amount of data.

Fix it with a Map, then verify twice

The fix is to use something that knows how to look up by key without scanning from the start. The Map of JavaScript does exactly that. You can read about Map in the MDN documentation; how fast it is, the table of measurements below shows. The new version replaces result.find(...) with byCustomer.get(customer):

src/report.ts (the new version)
import { orderTotal, type Order } from "./orders.ts";

export type ReportRow = { customer: string; orderCount: number; total: number };

// A report for the owner: how many orders each customer placed and how much they spent in total, the biggest spender first.
// Uses a Map to find the row of a customer in roughly constant time, instead of find, which has to scan from the start of the list.
export function reportByCustomer(orders: Order[]): ReportRow[] {
  const byCustomer = new Map<string, ReportRow>();
  for (const order of orders) {
    const customer = order.customer?.phone ?? "walk-in";
    const row = byCustomer.get(customer);
    if (row) {
      row.orderCount += 1;
      row.total += orderTotal(order);
    } else {
      byCustomer.set(customer, { customer, orderCount: 1, total: orderTotal(order) });
    }
  }
  return [...byCustomer.values()].sort((a, b) => b.total - a.total);
}

I keep the first version for comparison: rename the old file to src/report-old.ts, rename the function inside to reportByCustomerOld, and replace the declaration of the type ReportRow with an import type line that takes the type from the new file. Here is the changed file:

src/report-old.ts
import { orderTotal, type Order } from "./orders.ts";
import type { ReportRow } from "./report.ts";

// A report for the owner: how many orders each customer placed and how much they spent in total, the biggest spender first.
// The first version (kept to compare results), written to be easy to read: for each order, look for the row of that customer in the result.
export function reportByCustomerOld(orders: Order[]): ReportRow[] {
  const result: ReportRow[] = [];
  for (const order of orders) {
    const customer = order.customer?.phone ?? "walk-in";
    const row = result.find((r) => r.customer === customer);
    if (row) {
      row.orderCount += 1;
      row.total += orderTotal(order);
    } else {
      result.push({ customer, orderCount: 1, total: orderTotal(order) });
    }
  }
  return result.sort((a, b) => b.total - a.total);
}

First verification: is the result right? Before believing any number about speed, I want to be sure the fast version is not wrong. The test below compares the two versions on 3,000 orders (including the order of customers with equal totals), and checks three more small cases:

src/report.test.ts
import { describe, expect, test } from "vitest";

import { makeOrders } from "./make-data.ts";
import type { Order } from "./orders.ts";
import { reportByCustomer } from "./report.ts";
import { reportByCustomerOld } from "./report-old.ts";

const order = (id: number, phone: string | undefined, price: number): Order => ({
  id, createdAt: "2026-11-03T08:00:00+07:00", source: "web",
  ...(phone ? { customer: { name: "Customer", phone } } : {}),
  items: [{ name: "Coffee", price, quantity: 1 }],
});

describe("reportByCustomer", () => {
  test("groups orders by phone number, with the right total, the biggest spender first", () => {
    const orders = [order(1, "0901", 25000), order(2, "0902", 70000), order(3, "0901", 25000)];
    expect(reportByCustomer(orders)).toEqual([
      { customer: "0902", orderCount: 1, total: 70000 },
      { customer: "0901", orderCount: 2, total: 50000 },
    ]);
  });

  test("counter sales (no customer) are grouped into one row", () => {
    const orders = [order(1, undefined, 25000), order(2, undefined, 29000)];
    expect(reportByCustomer(orders)).toEqual([{ customer: "walk-in", orderCount: 2, total: 54000 }]);
  });

  test("an empty list gives an empty report", () => {
    expect(reportByCustomer([])).toEqual([]);
  });

  test("the Map version gives the same result as the first version on 3,000 orders (including the order of ties)", () => {
    const orders = makeOrders(3_000, 1_200);
    expect(reportByCustomer(orders)).toEqual(reportByCustomerOld(orders));
  });
});
Run the tests
npx vitest run src/report.test.ts
Output
 Test Files  1 passed (1)
      Tests  4 passed (4)

Second verification: how much faster is it? The script below runs each version five times at four data sizes and prints the median (the middle value, so one noisy run does not skew the result):

compare-runs.ts
// Compare the first version and the Map version: each runs 5 times on the same data, and the median time is printed (the middle value).
// The sizes are small enough for the old version to finish, so you can see how the time grows with the number of orders. Run: node compare-runs.ts
import { reportByCustomer } from "./src/report.ts";
import { reportByCustomerOld } from "./src/report-old.ts";
import { makeOrders } from "./src/make-data.ts";

function median(run: () => unknown, runs = 5): number {
  const times = Array.from({ length: runs }, () => {
    const start = performance.now();
    run();
    return performance.now() - start;
  }).sort((a, b) => a - b);
  return times[Math.floor(runs / 2)];
}

for (const orderCount of [5_000, 10_000, 25_000, 50_000]) {
  const orders = makeOrders(orderCount, Math.round(orderCount * 0.4));
  const old = median(() => reportByCustomerOld(orders));
  const fresh = median(() => reportByCustomer(orders));
  console.log(`${String(orderCount).padStart(6)} orders: first version ${String(Math.round(old)).padStart(5)} ms, Map version ${fresh.toFixed(1).padStart(5)} ms`);
}
Compare the two versions
node compare-runs.ts
Output (the numbers on your machine will differ)
  5000 orders: first version    25 ms, Map version   0.8 ms
 10000 orders: first version    65 ms, Map version   1.3 ms
 25000 orders: first version   427 ms, Map version   3.3 ms
 50000 orders: first version  1710 ms, Map version   8.3 ms

Look at the first-version column. From 5,000 to 50,000 orders, which is ten times more, the time grows from 25 ms to 1,710 ms, nearly seventy times. That is the sign of time growing with the square of the data. The Map version grows almost linearly: ten times the data, about ten times the time. At 50,000 orders the new version is about 200 times faster. Your exact numbers will differ from machine to machine, but the shape of the two columns will not change.

How far should you optimize?

Look at the first line of the table: at 5,000 orders the first version takes only 25 ms. For a café with a few dozen orders a day, the first version would run fine for years, and writing it in the easy-to-read way was a reasonable decision. In my view, nobody should write a Map everywhere to be safe. What is worth doing is to measure when there is a sign of slowness, such as the 1,984 ms log line at the top of the post, and then fix exactly the place that the measurement points at.

Three habits I keep after this post. First, always have a number before you fix, so you know whether it got better afterwards. Second, profile before you guess. Third, keep one working version to compare results against, because an optimization that changes the result is not an optimization.

I should also state the limits clearly: the measurements in this post ran on my machine, with fake data with a fixed seed and one specific shape (20,000 customers over 50,000 orders). With real data or a different machine, the numbers will differ. The measuring process can be reused.

The number of orders grows, and there is an old CSV file to import

The report now runs in a few milliseconds, and the owner is happy. But the owner just mentioned something else: besides the data we already have, the old point-of-sale machine left behind a CSV file with thousands of old orders that never entered the system. That file was typed in by hand by many people over many years, so it is not consistent. How to import a file like that without breaking the data is the job of the next posts.

You can follow the whole story in the series Taking over an old project. If you want to review how to read an error, the post on how to read a stack trace is the place to start, and the questions about performance and Node.js are in the interview question bank.

Get the next post by email.

One email a week, only when there is something worth reading. No spam.