How to profile a ts-node script for performance optimisation!

I had to profile a script i run via ts-node, as something was running veryyy slowly. I’m probably going to forget how to do it again in the future, so here’s how to do it.

For this to work, I had to install ts-node globally by running the below. There’s probably a smarter way of doing it, but eh, it works 🤷‍♂️:
node -g install ts-node

We then run the Node.js v8 sampling profiler:

node -r ts-node/register --prof script.ts --arg1 true --arg2 false ...

This should result in a file named “isolate-something-something”. This is gibberish (to me at least), so we can process it with the following:

node --prof-process isolate-0x7facb9300000-1508-v8.log > processed.txt
If you open the new “processed.txt” file, you should now get a human readable summary. This article has some insights on how to interpret it: “easy profiling for Node.js Applications”. There’s probably better and smarter ways of doing this. But it worked for me! Now I just have to figure out how to fix line 192 in my script, so it isn’t so darn slow.