-2

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.

  • 7
    For almost all applications it absolutely does not matter. Worry about algorithmic efficiency and not pointless minutia like this. (And that should have been the answer to the interview question.) – Pointy Jun 09 '18 at 13:02
  • If `a.length` is non-negative then 2 is definitely faster... – David Jun 09 '18 at 13:02
  • 1
    Why can't you do some tests yourself? https://jsperf.com/ – Scott Marcus Jun 09 '18 at 13:03
  • @David lol I just noticed that – Pointy Jun 09 '18 at 13:04
  • @David Can you please explain why it is? – aditya shrivastwa Jun 09 '18 at 13:04
  • 1
    One thing to remember is that in 1 - `a.length` is checked each time. – Nigel Ren Jun 09 '18 at 13:04
  • 3
    @adityashrivastwa: Because loops which never execute are faster than loops which do execute. – David Jun 09 '18 at 13:04
  • "He" wanted you to answer "3" because 3 caches `a.length` so that it doesn't have to be calculated upon each loop iteration. – Scott Marcus Jun 09 '18 at 13:10
  • @David Sorry,It was my fault I didn't write the code properly.It's edited now and I think may be the Interviewer wants to trick me? – aditya shrivastwa Jun 09 '18 at 13:10
  • @ScottMarcus Yeah he told me 3rd one is the fastest and 2nd is equivalent to it and the 1st one is sowest. – aditya shrivastwa Jun 09 '18 at 13:15
  • @ScottMarcus "so that it doesn't have to be calculated upon each loop iteration" [Not really](https://stackoverflow.com/questions/6112361/is-javascript-str-length-calculated-every-time-it-is-called-or-just-once). – str Jun 09 '18 at 13:16
  • 1
    @adityashrivastwa The question you got asked is useless and the "correct" answer you got is, depending on the implementation, even wrong. – str Jun 09 '18 at 13:17
  • 1
    @str You're comment is based on the optimizations of one client and not the language in general. And, while I think we all agree that this is really not a question with any practical benefit, we can all understand that the question was asked to see if the interviewee can spot and understand such optimization opportunities. – Scott Marcus Jun 09 '18 at 13:33
  • 1
    @ScottMarcus Discussing performance on a theoretical level is even more useless then microoptimization. If optimization opportunities is what the question is about, then the interviewer did not get that either. There is no universally correct answer to the question. – str Jun 09 '18 at 13:36
  • 2
    @str You miss my point. I'm fairly confident the question was asked to solicit the exact kind of conversation that we are having in this thread. If the answer is "I don't know", then that is telling about the level of experience the interviewee has. If the answer is "Always 3", that's telling as well. If the answer is, "It really isn't a discernible difference, then that can spawn a conversation about why. No matter what answer is given, the interviewer will gain an understanding of the level of knowledge of the interviewee. – Scott Marcus Jun 09 '18 at 13:40
  • 1
    @ScottMarcus I did get your point, that is why I wrote "the interviewer did not get that either" as they claimed the third answer is the "correct" one :) I fully agree with your last comment but your first one seemed too absolute and might be wrong in some implentations. – str Jun 09 '18 at 13:56

1 Answers1

-1

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.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • "the second and third are [...] faster". [Or it might be the opposite](https://stackoverflow.com/questions/6112361/is-javascript-str-length-calculated-every-time-it-is-called-or-just-once). – str Jun 09 '18 at 13:30
  • @str that tests an entire different thing. But yes, this is premature optimization. – Jonas Wilms Jun 09 '18 at 13:34
  • The JSPerf example is about repetitive access of the string length which is exactly what you mentioned in your answer. – str Jun 09 '18 at 13:38
  • 1
    @str 1) testing with only 10 accesses makes no sense at all as most JIT engines will only inline after a certain number of calls 2) testing with code that does not do anything makes no sense either as the engine might actually does not exexcute it. 3) only accessing a length does not mean iterating. Other optimizations apply here 4) as it looks like inside the loop `str[i]` is probably accessed which needs to be taken into consideration too. => The testcase you linked doesnt say anything, and nothing concerning the question. My testcase isnt perfect either but probably a better estimation. – Jonas Wilms Jun 09 '18 at 13:41
  • @str okay, i revoke the testcase. By swapping the examples (running one before the other) it also swapped the timed results, so it seems like the difference was just some server warmup time. – Jonas Wilms Jun 09 '18 at 13:46
  • Those performance tests are always approximations and only real life measurements are actually useful. Thanks for the update on your answer, I think now it is a good response the the question (even though the question is sketchy). – str Jun 10 '18 at 17:23