Create a simple overview over your process.
- copy timer.ts (or timer.js when you are using javascript) into your apps source directory
- Customize the
writeFileSync
call in timer.js or timer.ts, instructions in theprint
method in theTimer
class - Use it by calling timer.start(name) before your thing you want to time (for example a function call) and the returned end method after it. Note: When you want to get usable results in the Caller entry, you have to call the start method in the function you want to time. For any non-function call, this field is useless, as it shows the function name that called the function that called the start() method.
Example
/* index.ts */ import { timer } from './timer'; function a() { const end = timer.start('a()'); // really complex code that might take a while end(); } function b(c: string) { const end = timer.start(`b("${c}")`); a(); a(); // other code end(); } function c() { const end = timer.start('c()'); b('a'); end(); } c(); // NOTE: You have to call timer.print in a function, and cant provide it as an argument. process.on('beforeExit', function () { timer.print(); });Output:
$ tsc $ node index.js c(): Xms (Start: XX:XX:XX AM|PM | End: XX:XX:XX AM|PM | CalledBy: Object.<anonymous> in index.js:26:1) b(a): Xms (Start: XX:XX:XX AM|PM | End: XX:XX:XX AM|PM | CalledBy: c in index.js:21:5) a(): Xms (Start: XX:XX:XX AM|PM | End: XX:XX:XX AM|PM | CalledBy: Object.<anonymous> in index.js:11:5) a(): Xms (Start: XX:XX:XX AM|PM | End: XX:XX:XX AM|PM | CalledBy: Object.<anonymous> in index.js:12:5)
- Add
process.on('beforeExit', function () { timer.print(); })
at the end of your mainfile. This will cause it to print and dump the profile.
- Open the index.html in your browser (Deployed Version)
- Copy the content of your profiler file (
profiling-<your apps name>-DD-MM-YYYY-HH-MM-SS.json
) - Paste it into the textarea
- Explore your profile.