otel-profiling-agent icon indicating copy to clipboard operation
otel-profiling-agent copied to clipboard

NodeJS: Support named arrow function

Open NimrodAvni78 opened this issue 5 months ago • 2 comments

when trying to profile code with named arrow functions, the name of the function is anonymous

some code examples:

function cpuIntensiveTask(n) {
    if (n <= 1) return n;
    return cpuIntensiveTask(n - 1) + cpuIntensiveTask(n - 2);
}

const cpuIntensiveArrowTask = (n) => {
    if (n <= 1) return n;
    return cpuIntensiveArrowTask(n - 1) + cpuIntensiveArrowTask(n - 2);
}

const result = cpuIntensiveTask(41);
const resultArrow = cpuIntensiveArrowTask(41);

in the resulting profiles i am seeing the cpuIntensiveTask function, but for the other half of profiles im seeing <anonymous>

is this something that is supported?

NimrodAvni78 avatar Jul 30 '25 14:07 NimrodAvni78

in the resulting profiles i am seeing the cpuIntensiveTask function, but for the other half of profiles im seeing <anonymous>

This is correct. An arrow function by definition is an anonymous function. You cannot name it, but you can assign it to a variable what is happening here. You could also assign the same lambda to other variables and pass it through to functions so it could have different variable names through which it gets called. There for that single block of code is unnamed, and it is just called via a named variable.

is this something that is supported?

Currently this is not supported. It might be non-trivial because the function name cannot be determined from the function block, but instead the call site would need to be inspected for the name of the variable used. I cannot immediately say if this is possible or not.

Does this work if you do a backtrace using the built-in node functions or other debugging/profling tooling?

fabled avatar Jul 30 '25 14:07 fabled

Image this is the backtrace from intellij IDE debugger for example, which recognizes it

also the stacktrace shown in case of a thrown error shows the arrow function name

Error: Simulated error at n=5
    at cpuIntensiveArrowTask (project/dist/index.js:24036:11)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)
    at cpuIntensiveArrowTask (project/dist/index.js:24039:10)

but when using the nodejs profiler it does list it as anonymous

   ticks parent  name
   2770   49.6%  LazyCompile: *cpuIntensiveTask project/dist/index.js:24029:26
   2770  100.0%    LazyCompile: *cpuIntensiveTask project/dist/index.js:24029:26
   2770  100.0%      LazyCompile: *cpuIntensiveTask project/dist/index.js:24029:26
   2770  100.0%        LazyCompile: *cpuIntensiveTask project/dist/index.js:24029:26
   2770  100.0%          LazyCompile: *cpuIntensiveTask project/dist/index.js:24029:26
   2770  100.0%            LazyCompile: *cpuIntensiveTask project/dist/index.js:24029:26

   2654   47.5%  LazyCompile: *<anonymous> project/dist/index.js:24034:52
   2654  100.0%    LazyCompile: *<anonymous> project/dist/index.js:24034:52
   2654  100.0%      LazyCompile: *<anonymous> project/dist/index.js:24034:52
   2654  100.0%        LazyCompile: *<anonymous> project/dist/index.js:24034:52
   2654  100.0%          LazyCompile: *<anonymous> project/dist/index.js:24034:52
   2654  100.0%            LazyCompile: *<anonymous> project/dist/index.js:24034:52

NimrodAvni78 avatar Jul 30 '25 19:07 NimrodAvni78