Skip to content
Tutorials Center
My learning

Topics

Lesson 111 of 133 ยท Production Node.js

Performance Tuning

Make Node.js code faster the right way - keep the event loop free, avoid sync I/O and JSON waste, stop regex ReDoS, benchmark honestly, avoid V8 traps and profile with real tools.

"Make it faster" is the most dangerous task in programming, because it invites guessing. Developers rewrite the loop they feel is slow while the real cost sits in a JSON call three layers away. Performance tuning is a discipline: measure, find the bottleneck, change one thing, measure again. This lesson gives you the Node-specific knowledge to do that well: what blocks the event loop, which everyday operations are surprisingly expensive, how to benchmark without fooling yourself, and which tools show where the time really goes.

Our editor cannot show reliable timings (they differ on every run and every machine), so the examples below demonstrate the causes with counts and yes/no checks instead. On your own machine you will add real measurements. Before you start, make sure you understand The Event Loop, and keep Performance Measurement and V8 and Memory handy.

Rule one: do not block the event loop

JavaScript in Node runs on one thread. While your code runs, nothing else does: no other request is answered, no timer fires, no socket is read. A function that takes 200 ms of CPU does not make one request slow; it makes every request wait up to 200 ms. At 100 requests per second, a handful of such calls keeps the server permanently busy and latency explodes.

So the first performance question is not "how fast is this function?" but "how long does it hold the thread?" The next example runs the same total work two ways. Alongside it, a setImmediate loop counts how many times the event loop got a chance to do other things.

Node.js One long task versus the same work in slices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The total work is identical. In the first case other work gets no turns until the whole task ends; in the second it is served between slices. (Exact numbers depend on timing, but the first count is always tiny and the second is at least ten.) For long CPU jobs slicing is a stopgap; the real fix is a worker thread or another process, as in Processing Large Data Efficiently.

Detecting a blocked loop

How do you know your server blocks? Run a heartbeat: a timer that expects to fire every 10 ms and records how late each tick is. If the lateness suddenly jumps, something held the thread. In production you use perf_hooks.monitorEventLoopDelay(), which keeps a histogram of the same thing and costs almost nothing, and you export its p99 to your metrics. Here is the idea in miniature, printing only yes/no answers so the output is stable.

Node.js A heartbeat that detects a blocked event loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

When the heartbeat reports trouble, find the culprit with a profiler (see the end of this lesson), not by guessing.

Avoid synchronous I/O in hot paths

Functions ending in Sync (readFileSync, execSync, pbkdf2Sync, existsSync in a loop) block the thread while the disk, child process or hash does its work. They are perfect at start-up, in CLI scripts and in build tools, where nothing else is waiting. They are wrong in a request handler: while one request reads a file synchronously, every other request freezes. Use the promise versions, as in fs.promises, and for CPU-heavy built-ins use the async variants (crypto.pbkdf2, scrypt) which run on the libuv thread pool.

A subtler habit is doing the same thing on every request: re-reading a template file, recompiling a regular expression, re-parsing a config. Do it once at start-up and keep the result in a variable, or cache it as in Scalability and Reliability Patterns.

JSON is not free

JSON.stringify and JSON.parse are heavily optimized, yet they must visit every byte, and they run on your main thread. A 5 MB response body costs milliseconds of CPU each time. Typical wastes:

  • Serializing the same object on every request. If a response changes rarely, stringify once and cache the string.
  • Sending fields nobody reads. Select the columns and properties you need; a smaller payload is cheaper to build, send and parse on the client.
  • Deep-cloning with JSON.parse(JSON.stringify(x)). Use structuredClone (and it handles Dates, Maps and more), or avoid cloning by treating data as immutable.
  • Pretty-printing in production. JSON.stringify(x, null, 2) makes the output larger for no reader.
Node.js Cache the serialized string and trim the payload

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The payload shrinks by a factor of about 10 and the CPU work by a factor of 100. When you really must serialize huge structures on every request, consider a schema-based serializer (such as fast-json-stringify, an npm package) or sending data in a streaming format; see Working with JSON.

Regular expressions and ReDoS

Some regular expressions have a terrifying property: on certain inputs the engine backtracks exponentially. A pattern like /^(a+)+$/ looks harmless, but give it "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!" and it tries every way to split the as between the inner and outer + before concluding there is no match. Each extra character doubles the time. An attacker who finds such a pattern in your validation code can freeze your whole server with one small request. This is ReDoS (regular expression denial of service).

Dangerous shapes are nested quantifiers (a+)+, (a*)*, and alternations that overlap, (a|a)+ or (.*a){10}, followed by something that can fail. Because the regex engine runs on the main thread, the entire process stops. The example runs the nasty pattern inside a worker so that we can safely give up on it after 300 ms.

Node.js A catastrophic regex, contained in a worker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Both patterns accept exactly the same strings, but the rewrite has no nested quantifier, so it runs in a single pass. To stay safe: avoid nested quantifiers, keep user-controlled input short (check input.length first), prefer simple parsing code over one giant regex, and never build a regex from untrusted text without escaping it. Node 24 does not have a built-in regex timeout, and the v flag does not change backtracking, so a worker, a length cap, or a linear-time engine (npm's re2) are your options for risky patterns.

Common validators copied from the internet (emails, URLs, "strong password" checks) are frequent ReDoS sources. If a regex has a quantifier inside a group that itself has a quantifier, treat it as suspect and test it with long non-matching input.

Benchmarks that do not lie

Microbenchmarks are easy to write and easy to get wrong. A loop that calls a function and ignores the result may be optimized away entirely by V8 (dead-code elimination). The first runs execute slowly in the interpreter before the JIT compiles hot code (warm-up). A single run is noise. A rough checklist:

  1. Warm up before you measure.
  2. Use the result (add it to a variable you print) so the work cannot be removed.
  3. Repeat and take the median or minimum, not one run.
  4. Compare on realistic data sizes; an algorithm that wins on 10 items may lose on 10,000.
  5. Change one thing at a time and look at relative differences, never absolute numbers from another machine.
  6. For serious work use a tool: node --cpu-prof, tinybench or mitata (npm packages), or the built-in perf_hooks.

The program below applies the checklist to a classic question: finding a value in a large array with includes versus a Set. It prints only whether the Set won by a wide margin, because exact times vary, but a 10x gap on 20,000 items is not noise.

Node.js A careful microbenchmark: Array.includes vs Set.has

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The lesson is the pattern, not the answer: pick the right data structure first (that is where 10x wins hide), and only then think about micro-optimizations that gain a few percent.

V8 optimization traps

V8 turns hot functions into machine code using assumptions about your data. Code that keeps those assumptions true stays fast; code that breaks them gets "deoptimized". Practical guidance:

  • Keep object shapes stable. Create objects with the same properties in the same order (use a class or a factory function), instead of adding properties later. Avoid delete obj.key; set it to undefined or null or use a Map.
  • Keep types stable. A function that is called with numbers, then with strings, then with objects is slower than one that always sees the same kind. Do not mix integers and arbitrary values in one array if you can help it.
  • Avoid holes and huge sparse arrays. new Array(1_000_000) creates a holey array; fill it, or use Array.from/typed arrays for numbers.
  • Use typed arrays for numeric data, see Typed Arrays.
  • Do not allocate in tight loops when you can reuse a buffer or object; fewer short-lived objects means less garbage collection.
  • Prefer simple code. Fancy micro-tricks age badly as V8 changes; clean, predictable code is what the optimizer is tuned for.

Treat these as tie-breakers for measured hot spots, not as rules for the whole code base. Most of your code is not hot.

A profiling workflow

When the real service is slow, work like this:

  1. Define the symptom with a number: p99 latency of /search is 900 ms; target 200 ms.
  2. Reproduce under load with a tool such as autocannon or k6, on production-like data.
  3. Profile to see where time goes. The CPU profiler built into Node writes a file you can open in Chrome DevTools or the VS Code profile viewer. Flame graphs show the widest boxes first, and those are your suspects.
  4. Fix the biggest box, then repeat. Usually one or two fixes give most of the win.
  5. Guard the result with a benchmark or a latency alert so it does not creep back.

These commands run on your machine, not in our editor (read-only):

Terminal
# 1. Write a .cpuprofile when the process exits, then open it in Chrome DevTools
node --cpu-prof --cpu-prof-dir=./profiles server.js

# 2. Attach DevTools live: open chrome://inspect and click "inspect"
node --inspect server.js

# 3. Generate load while profiling (npm package, run with npx)
npx autocannon -c 50 -d 20 http://localhost:3000/search?q=lamp

# 4. Heap snapshot / allocation hunting for memory growth
node --heap-prof server.js

Look at three kinds of signals together: CPU (which functions are on the stack most), event-loop delay (is the thread blocked), and memory (is the heap growing between GCs). A slow service with idle CPU is usually waiting on I/O: a slow query, an unbounded fan-out of HTTP calls, a missing index. A slow service with a pegged CPU needs the profiler. Database work often dominates; see Databases and Caching Strategies.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is a 200 ms synchronous function in a request handler a problem for other users too?

    While the thread is busy, no other callback runs, so one slow synchronous call adds its duration to everyone's latency.
  2. 2. Which fix removes the ReDoS risk of the pattern /^(a+)+$/?

    Nested quantifiers cause exponential backtracking. Removing the nesting keeps the same accepted strings but needs a single linear pass.
  3. 3. What is the main reason to call the result of a benchmarked function and keep it?

    If the result is unused, the optimizer may drop the computation, and you end up measuring nothing.
  4. 4. A response body that rarely changes is rebuilt with JSON.stringify on each request. What is a good improvement?

    Serialization costs CPU on the main thread every time. Reusing the cached string turns many calls into one.
  5. 5. Your service is slow but its CPU is mostly idle. What is the most likely cause?

    Idle CPU with high latency means the time is spent waiting, usually on a database, network call or lock. A CPU profiler would show little.

Summary

  • Ask how long code holds the thread, not just how fast it is; detect blocking with a heartbeat or monitorEventLoopDelay.
  • Keep Sync APIs out of request handlers, and do repeated setup work once.
  • Cache serialized JSON, trim payloads, and prefer structuredClone over JSON round-trips.
  • Avoid nested quantifiers in regexes, cap input length, and isolate risky patterns in workers or a linear-time engine.
  • Benchmark with warm-up, used results, repetition and realistic data; choose better data structures before micro-tuning.
  • Keep object shapes and types stable in hot code, and profile with --cpu-prof and flame graphs before changing anything.

Next: Project: Command-Line To-Do App