josefa josefa - 24 days ago 8
Javascript Question

1) Performance Impact on function inside a function 2) Order or flow in multiple conditions

Two question:

1) I want to know the performance impact on the last function(

LayerTen()
) in something like this:

function execit(){
LayerOne();
}
function LayerOne(){
LayerTwo();
}
// and so on, till:
function LayerNine(){ //thanks for correcting, Mike
LayerTen();
}


I know, of course, that it depend what is actually inside the functions, but is this structure of them may cause performance issues anyhow?

2) If multiple, but separately conditions are made like in the following example, what would be the flow of it: will they execute all at once or one after another?

if(condition one){
code
}else{code}
if(condition two){
code
}else{code}
if(condition three){
code
}else{code}
// and so on, till:
if(condition thirty){
code
}else{code}

Answer

Javascript is single threaded. So the only perfomance issue is the memory consumption. While an if else does not consume much memory, a function stays in the memory until it completely finished:

Function execit(){ 
//memory usage started
LayerOne(); 
//memory is recycled
} 
function   LayerOne(){
//already two opened functions...
 LayerTwo();
 //memory is recycled
 }
 // and so on, till: 
function LayerTen(){
//ten functions in memory
 LayerTen(); 
 //memory recycled
 }

So it puts 10 functions in memory, before they all after another are recycled. This would be better:

function start(){
 a();//a is right now recycled
 b();//b is right now recycled
 }

However it depends on your actual code if this is possible...