How Node really works · Chapter 28 of 34
Debugging and performance
By the end of this chapter you can measure where a Node program spends its time and memory, and prove a fix made it faster. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The system
- 5 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
The nightly report used to finish in two seconds. Now it takes forty, and while it runs the order server answers nothing: riders refresh the app and Anjali's inbox fills with "is Tapri closed?". Nobody changed the server. Somebody changed the report.
Guessing does not find the slow line. Adding console.log(Date.now()) everywhere gives whole milliseconds, one noisy run, and a lot of lines to delete afterwards. Node has a clock, a profiler hook, a memory gauge and a debugger built in.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
timeIt: time any job, sync or async, with performance.now
Write timeIt(fn). It calls fn once and returns a promise of { result, ms }: result is what fn returned (for an async fn, the value its promise resolved to) and ms is how many milliseconds passed from just before the call until the result was ready, measured with performance.now (a number with fractions, never negative).
A job that waits 60 ms for a timer must measure at least about 60 ms. When fn throws or its promise rejects, timeIt rejects with that same error.
- Task 2
profile: rank the report functions from slowest to fastest
Write profile(fns, runs), with runs 5 when it is left out. fns is an object of name to function. Time each function with performance.timerify and its own histogram from createHistogram, calling the wrapped function runs times with no arguments.
Return an array with one row per function, { name, calls, meanMs }, where calls is how many calls the histogram recorded and meanMs is the histogram's mean in milliseconds (the histogram records nanoseconds). Sort the rows slowest first, by meanMs.
- Task 3 · interview question
Interview: measureBlock, measure how long a busy loop blocks the event loop
Write measureBlock(work). It schedules a timer of 0 ms, then calls work once, synchronously, before measureBlock returns. It returns a promise of how many milliseconds passed between scheduling the timer and the timer's callback running, measured with performance.now.
When work keeps the thread busy for 120 ms the answer is at least about 120; with work that does nothing it is close to 0.
- Task 4
dailySummary: make the daily summary fast without changing its answer
Rewrite dailySummary(orders). Each order is { customer, total }. It returns one row per customer, { customer, orders, total }: how many orders they placed and the sum of their totals. Rows are sorted by total, biggest first, and ties by customer name A to Z (byTotalThenName at the top of the file does exactly that). An empty list gives [].
The answer must stay exactly the same as the slow version's, and for 4000 orders from 2000 customers it must take less than a fifth of the slow version's time.
- Task 5
measureHeap: measure how much memory a job holds on to
Write measureHeap(fn). It calls fn once and returns, straight away (not a promise), { result, bytes }: result is what fn returned, and bytes is how much the heap grew, process.memoryUsage().heapUsed read after the call minus the same reading before it, while the result is still held.
For an array of 2,000,000 numbers with fractions, bytes is more than 8,000,000; for a short string it is well under 1,000,000.
What you will learn
- performance.now: a precise clock for timing any job.
- timerify and histograms: timing every call of a function.
- Event loop delay: how long blocking work holds everyone up.
- Algorithms and memory: the loop inside a loop, process.memoryUsage and the inspector.
More in The system
Build "Debugging and performance" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.