My JS program is slow, I want to find functions that consume most CPU time.
How can I do that? No precise timing or advanced charting needed, just a rough estimate to find names of couple most time consuming functions.
Example – the script contains a problematic function c
the profiler should help to find it:
function a() {
const r: string[] = []
for (let i = 0; i < 500; i++) r.push(b(i))
return r
}
function b(n: number): string {
return c(n)
}
const letters = 'abcdefghijklmnopqrstuvwxyz'
function c(n: number): string {
const b: string[] = []
for (let i = 0; i < n; i++) {
b.push(letters[i % letters.length])
b.sort() // <= Problem
}
return b.join('')
}
function run () { a() }
;(globalThis as any).run = run
P.S. I tried using built-in bun.js debugger, but it has no CPU hotspot information. I tried bun --inspect main.ts
and then switching to “Timelines” and clicking on ‘Record’ button and then executed run()
in the console, and when it finished clicked ‘Stop recording’. It created the dump with statistics, but I have not found any CPU timing information.