13

I read Is there a performance difference between i++ and ++i in C?:

Is there a performance difference between i++ and ++i if the resulting value is not used?

What's the answer for JavaScript?

For example, which of the following is better?

  1. for (var i = 0; i < max; i++) {
      // code
    }
    
  2. for (var i = 0; i < max; ++i) {
      // code
    }
    
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Oriol
  • 274,082
  • 63
  • 437
  • 513

2 Answers2

15

Here is an article about this topic: http://jsperf.com/i-vs-i/2

++i seems to be slightly faster (I tested it on firefox) and one reason, according to the article, is:

with i++, before you can increment i under the hood a new copy of i must be created. Using ++i you don't need that extra copy. i++ will return the current value before incrementing i. ++i returns the incremented version i.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
-5

No. There is no difference in execution time. The difference in the two code snippets is when i gets incremented.

for(i = 0; i < max; i++)
{
    console.log(i);
}

This first example will yield the results: 0,1,2,3,...,max-1

for(i = 0; i < max; ++i)
{
    console.log(i);
}

This second example will yield the results: 1,2,3,...,max

i++ increments the value after the operation. ++i increments the value before the operation.

There is no performance difference other than the one less iteration it will make on ++i because the increment is done before the first operation

scottmgerstl
  • 765
  • 8
  • 18
  • I'm not sure why the down-vote. The JSPerf linked-to in jidma's answer shows that the two perform the same. – Jasper Sep 20 '12 at 00:54
  • Agreed, well within 1% of each other. ++i shows to be 1% slower in Safari 6.x. A good example to explain the difference is `var i = 0; alert(++i);` vs `var i = 0; alert(i++);` – Mark Sep 20 '12 at 00:55
  • 4
    -1. Both codes print the same thing. – Karoly Horvath Sep 20 '12 at 00:56
  • 1
    http://jsbin.com/oninoy/1/edit – elclanrs Sep 20 '12 at 00:57
  • DeveloperKlin: In a loop the assertion that you make about the value if `i` is invalid, it is however valid when taken in other contexts, like concocting a string. – Jasper Sep 20 '12 at 00:59
  • 1
    @KarolyHorvath i agree, in order to have different results it should be `for(i = 0; i < max; ) console.log(++i)` vs `for(i = 0; i < max; ) console.log(i++)` – Majid Laissi Sep 20 '12 at 00:59
  • @elclanrs here is what he meant http://jsbin.com/oninoy/2/edit – Majid Laissi Sep 20 '12 at 01:01
  • I checked out what you guys had said and it appears you are right. Thanks for the clarification. – scottmgerstl Sep 20 '12 at 01:01
  • Yeah, in a loop statement it doesn't make a difference in the output because it gets evaluated after the iteration. You have to use it inside to see the difference like others said. – elclanrs Sep 20 '12 at 01:03
  • Is "console.log" slow enough to hide the difference between ++i and i++ ? – Qi Fan Apr 26 '16 at 21:20
  • @Nateowami: Just wonder if it is meaningful to time a loop with irrelevant function calls that may be too slow than the thing to be measured (i++ or ++i). What if console is a slow serial port or punch card? – Qi Fan Aug 31 '16 at 21:02
  • @Qi Fan Oh, I see what you were saying. My mistake. Just deleted it. – Nateowami Sep 01 '16 at 01:14