Is there a performance difference between i++ and ++i in JavaScript? [closed]
Asked Answered
P

2

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
    }
    
Penitence answered 20/9, 2012 at 0:45 Comment(5)
Not enough to matter to you. I tend to prefer the ++i notation unless it's explicitly wrong for the use case.Overpower
with any decent js engine it should be identicalJaniuszck
i hear there's a jsperf.com but not sure how to use it.Goon
Pretty sure this is trivial for real performance.Flashcube
Duplicate of #1547481Parkin
G
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.

Gamekeeper answered 20/9, 2012 at 0:51 Comment(4)
This is really more of a comment. An answer would explain how that link answers the question.Offprint
@jasper i was coming to that :)Gamekeeper
it depends on your browser. ++i is slower in safari but faster in some browsers.Electrician
"under the hood a new copy of i must be created" - only if you use the result...Janiuszck
P
-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

Polygynist answered 20/9, 2012 at 0:53 Comment(12)
I'm not sure why the down-vote. The JSPerf linked-to in jidma's answer shows that the two perform the same.Offprint
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++);Electrician
-1. Both codes print the same thing.Janiuszck
jsbin.com/oninoy/1/editFlashcube
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.Offprint
@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++)Gamekeeper
@Flashcube here is what he meant jsbin.com/oninoy/2/editGamekeeper
I checked out what you guys had said and it appears you are right. Thanks for the clarification.Polygynist
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.Flashcube
Is "console.log" slow enough to hide the difference between ++i and i++ ?Manifestation
@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?Manifestation
@Qi Fan Oh, I see what you were saying. My mistake. Just deleted it.Adenosine

© 2022 - 2024 — McMap. All rights reserved.