What is clinic.js?
Clinic.js is a set of tools for monitoring and profiling Node.js applications written in TypeScript. It allows developers to identify performance bottlenecks, memory leaks, and Event Loop issues. The library provides visual reports in the form of flamegraph, bubbleprof, and heapprofiler.
Why is TypeScript monitoring needed?
TypeScript compiles to JavaScript, but at the execution level it is the same Node.js. Without monitoring, it is impossible to understand why an application slows down under load. Clinic.js helps:
- Detect slow functions and asynchronous operations
- Analyze memory usage
- Optimize the Event Loop (for example, when working with heavy computations)
- Compare performance before and after refactoring
Installation
Install clinic.js globally via npm:
npm install -g clinicTo use it in a TypeScript project, also install the types:
npm install --save-dev @types/nodeMain Features
- clinic doctor — general application health diagnostics (Event Loop, memory, CPU)
- clinic flame — creates a flame graph for call stack analysis
- clinic bubbleprof — visualization of asynchronous operations (promises, callbacks)
- clinic heapprofiler — heap profiling to find memory leaks
Usage Example with TypeScript
Suppose you have an Express application with TypeScript. Run it with clinic doctor:
clinic doctor -- node dist/index.jsAfter finishing the work (Ctrl+C), an HTML report will open. Example code for testing:
import express from 'express';const app = express();
app.get('/heavy', (req, res) => { const arr = []; for (let i = 0; i < 1e6; i++) { arr.push(Math.random()); } res.send('Done');});
app.listen(3000, () => console.log('Server running'));Then load it using autocannon:
npx autocannon -c 10 -d 10 http://localhost:3000/heavyThe report will show that the loop blocks the Event Loop.
When to Use
- When performance drops in production
- Before scaling an application
- When refactoring critical code sections
- For optimizing asynchronous flows (for example, when using RxJS)
Conclusion
Clinic.js is a must-have tool for any TypeScript developer working with Node.js. It replaces manual logging and helps quickly find problems that cannot be caught in a debugger.