Reduce Javascript Function Overhead

I saw Javascript code that claims to speed up function calls, for example:

function foo() { // do something } function myFunc() { var fastFoo = foo; // this caches function foo locally for faster lookups for (var i = 0; i < 1000; i++) { fastFoo(); } } 

I don’t see how this can speed up work on calling the javascript function, since it seems to me that this is just a search in memory, either at the top of the current stack (for fastFoo), or somewhere else on the stack (I am not sure where global context stored ... anyone?).

Is this a relic of ancient browsers, a complete myth or a true improvement improver?

+4
source share
2 answers

It all depends on the volume. Access to the local area is always faster than access to the parent area. if the function is defined in the parent area, you will often see accelerations if you make a local link.

If this acceleration is significant, it depends on many factors, and only testing in your case will show whether it is worth doing.

The difference in speed depends on the difference in volume.

Call abcdefgh(); from xyz scope is slower than calling ab(); from the abc domain (not the most beautiful or the most correct examples, but this should be the goal :)

+2
source

This will lead to infinitely low performance.
Do not do that.

+2
source

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


All Articles