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")
}
2
  • duplicate: Get function name in JavaScript Commented Feb 23, 2021 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. Commented Feb 24, 2021 at 9:23

1 Answer 1

1

Use arguments.calle.name:

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

my_function_name();

Sign up to request clarification or add additional context in comments.

2 Comments

Won't work in strict mode, though.
Is there a proper way?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.