I know, in c++, ++i is better then i++.
++i; // Fetch i, increment it, and return it
i++; // Fetch i, copy it, increment i, return copy
Here's same question about c++.
So, what about javascript ?
I know, in c++, ++i is better then i++.
++i; // Fetch i, increment it, and return it
i++; // Fetch i, copy it, increment i, return copy
Here's same question about c++.
So, what about javascript ?
Taken from Guffa:
The difference between i++ and ++i is the value of the expression.
'The value i++
is the value of i
before the increment. The value of ++i
is the value of i
after the increment.
Example:
var i = 42;
alert(i++); // shows 42
alert(i); // shows 43
i = 42;
alert(++i); // shows 43
alert(i); // shows 43
The i--
and --i
operators works the same way.'
So basically, the only difference is the result of the increment.
Unless you're using the result of the increment, either in an assignment or as part of an expression, there's absolutely no difference between pre- and post-increment.
If you're using the result, pre-increment may be slightly faster because it doesn't need to save the old value while it's incrementing. But if you're using the result, you need to choose which form to use based on which value you need to assign -- you can't usually make the decision based on performance.