Which of these two bits executes (counts) faster?
var i = 0;
while(true){
i++;
}
or
var i = 0;
inc = function(){
i++;
inc();
}
Does the preferred way change if the looped code / function gets longer?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This will only give you a stack overflow as there is no end condition for the recursion :
For a great enough
i, this will fail faster than a standard loop would do.More generally, there is greater overhead in calling a function than just looping. Make a function when it helps your code being reusable, or readable. A loop is fast.