1

How can I get the wrapping function name of my code? I want to implement a runtime-tracking feature in many functions of my project and don't want to replace the function name in every function by myself.

function my_function_name() {
  let t0 = performance.now();
  ....
  DEBUG_TRACK_RUNTIME(performance.now() - t0)
}

function DEBUG_TRACK_RUNTIME(runtime) {
  console.log("PROCESS RUNTIME: " + this.caller, Math.round(runtime, 0) + " ms")
}

Is there a way to get the function name "my_function_name()" as a string?


QUESTION ANSWERED. This solution worked great for me. Thanks

function abcd() {
    let t0 = performance.now();
    ...
    DEBUG_TRACK_RUNTIME(arguments.callee.name, performance.now() - t0)
}

function DEBUG_TRACK_RUNTIME(title, runtime) {
    console.log("PROCESS RUNTIME: " + title + "()", Math.round(runtime, 0) + " ms")
}
gear
  • 181
  • 1
  • 11
  • duplicate: [Get function name in JavaScript](https://stackoverflow.com/questions/3178892/get-function-name-in-javascript) – szaman Feb 23 '21 at 18:23
  • Maybe you should not build complex generic reusable features based on `arguments.callee`. It won't work in strict mode. In modern browsers JS code is always run in strict mode in modules and class declarations. – Teemu Feb 24 '21 at 09:23

1 Answers1

1

Use arguments.calle.name:

function my_function_name() {
  console.log(arguments.callee.name);
}

my_function_name();
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71