Skip to content
Advertisement

Does automatically hoisting slow down the performance of JavaScript?

Lately, I was studying Scope in Javascript. I want to know whether automatically hoisting is done at compile time or at the time of executing the code(run time). If it does at run time then I have another question does auto-hoisting will slow down the performance of the Javascript program.

something = a(); 
function a(){
 console.log("hoisting");
 return 10;
}
var something; 

Should we use manual hoisting or it would be better to use automatically hoisting?

Advertisement

Answer

To put my comments as an answer:

People have a different understanding of what hoisting supposed to mean. Fact is that, according to the spec, every time a function is called a new execution context is created, which holds a new environment. Then the function body is processed to find all variable declarations (var, let, const (and function declarations)) and bindings for those names are created in the new environment. var declarations are initialized with undefined. Then the body is actually evaluated.

Considering this, from the perspective of the engine it doesn’t really matter where you place the var declaration, the whole body has to be processed anyway.

Having said that, I would be surprised if actual implementations didn’t cache that information. After all, the variable declarations in a function don’t change between function calls.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement