admin管理员组

文章数量:1400591

Lately, I was studying Scope in Javascript. I want to know whether automatically hoisting is done at pile 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?

Lately, I was studying Scope in Javascript. I want to know whether automatically hoisting is done at pile 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?

Share Improve this question edited Jan 16, 2021 at 8:57 AbsoluteBeginner 2,2533 gold badges14 silver badges24 bronze badges asked Jan 16, 2021 at 8:05 Subrato PattanaikSubrato Pattanaik 6,0698 gold badges25 silver badges60 bronze badges 9
  • 2 The specification is pretty clear about what happens when a function is evaluated. The hoisting happens when the new execution context for the function is created. I.e. it will happen every time the function is called and it doesn't matter where you place the var statement. Per spec, the engine has to parse the function for all var statements anyway. Having said that, I'm sure real implementations probably only do that once and keep a "canonical" representation of the function around, instead of the original one (and who knows what happens with JIT pilation anyway). – Felix Kling Commented Jan 16, 2021 at 8:19
  • 1 What do you mean with "manual hoisting"? – trincot Commented Jan 16, 2021 at 8:22
  • 1 According to the spec, tc39.es/ecma262/#sec-functiondeclarationinstantiation happens on every function call, not at parse time. But as I already said, there are definitely optimizations an implementation could do, though that's not my area of expertise. It certainly seems to be reasonable to collect information about all variable declarations inside the function once. – Felix Kling Commented Jan 16, 2021 at 8:27
  • 1 Also re hoisting: People have a different understanding of what exactly it's supposed to mean. Fact is that, according to the spec, when 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. – Felix Kling Commented Jan 16, 2021 at 8:29
  • 1 @SubratoPatnaik - I like your question as you seems a curious person, just want to say that the result of this inquiry would not affect the perceived performance of any program. – vsync Commented Jan 16, 2021 at 8:39
 |  Show 4 more ments

3 Answers 3

Reset to default 5

To put my ments 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.

As I know, There are no performance issues. The initializations are getting done in pile time. So doesn't matter you initialize on top or bottom, The JS engine will create the references in pile time.

BUT

If you forgot to initialize at the bottom, It will be initialized as undefined by default. Because of hoisting it’s considered a practice to declare functions or variables at the top of their respective scopes.

JavaScript: What is Hoisting? (Remended)

It is not done at run time. It's in the pile process. So it doesn't slow down the performance. Just before the code is executed the piler scans for all variable and function declarations and allocates them in the memory.

本文标签: Does automatically hoisting slow down the performance of JavaScriptStack Overflow