javascriptperformanceoperatorspost-incrementpre-increment

Is there a performance difference between i++ and ++i in JavaScript?


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
    }
    

Solution

  • 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.