Well,I went for interview today and there Interviewer asked me a question and it's like this.
let a = "Aditya";
1.for (let i=0; i<a.length; i++)
2.for(let i=a.length; i>0; i--)
3.for(let i=0, length=a.length; i<length; i++)
which of the above for loop runs faster 1 , 2 , 3? Arrange it, and give the reason as well. I was like well I used (1) most the so it could be the faster one but he told me I was wrong and I tried google couldn't find any helpful solution.Please help me out.
The best answer is probably:
Why do you even care? Choose the one that is the most readable. Unreadable code takes more time for people to understand, and the benefits of a few milliseconds faster do not matter. If such small performance things matter, you should be writing assembly.
He told me 3rd one is the fastest and 2nd is equivalent to it and the 1st one is slowest.
Not really. Modern browsers do optimize all three loops so much that they actually run with the same speed.