What is the consensus regarding using Function.caller in JavaScript?

Some time ago I read that you should not use Function.caller inside a function, because it makes the function non-built-in. To verify this statement, I wrote the following guideline:

Does Function.caller affect shaping? JsPerf .

The results show that using Function.caller does make function execution slower than usual:

  • In Opera, it is 16% slower.
  • In Chrome, it is 80% slower.
  • In Firefox, it is 100% slower.

Therefore, my question is: what is the consensus on using Function.caller in JavaScript? Is it right to use? Should this be avoided at all?

+4
source share
2 answers

As far as I know, dynamic control of the execution stack with caller / callle / etc is not allowed in strict mode, so you can see that this function should be avoided by consensus if possible.

Anyway, why do you want to use Function.caller at all? This makes your code depend on what usually does not matter (call stack), and the data is passed implicitly, not through explicit arguments. The only real use I've ever seen for this feature is to trace stack stacks, in which case you can usually pay the execution cost or you can bypass it using the debugger.

+2
source

If performance is your only concern, this is probably great. Despite the fact that we are much slower than not referring to caller , my machine can still do it 1.6 million times per second.

Slow may be a relative term. If you just need to call him rarely, he does it fast enough, most of the time. I just wouldn't put it in a big loop, iterating over every frame of the animation in my game.

However, this magical property has other problems. There are more issues than just performance, as @missingno points out.

+1
source

Source: https://habr.com/ru/post/1497345/


All Articles