i++ vs. ++i in a JavaScript for loop
Asked Answered
F

4

10

Because of JSLint, I almost always use i += 1 to increment a JavaScript for loop, but for quick and dirty scripts, I use i++ instead.
However, I see a lot of for loops in other people's code in which they increment i by doing ++i instead.

As far as I know, there is no difference in meaning between i++ and ++i, and jsPref shows no difference in performance.
As such, I'm wondering where the convention of doing ++i comes from and why people tend to do it.

Does anyone know why a lot of JS coders tend to prefer ++i over i++ when incrementing the counter in a for loop?
Thanks.

Fallfish answered 27/4, 2015 at 1:24 Comment(2)
It completely dosent matter, incrementing part executes after block of code and then condition is reevaluated. So regardless of what's returned by i++ or ++i has no effect.. however i used to know what drove everyone to use ++i .. it was some performance tip from some other language.Rancourt
Basically idea was that i++ is less efficent because it caused a copy to be created in memory. One that is returned and original one that is increased. Whereas, ++i meant just increase the original one then return it...however with smarter compilers this is a non issueRancourt
P
5

In JS and PHP it does not make any difference, I think even in Java it does not make any difference but in pure c when compiler is not optimizing code it does, and that is why a lot of people use ++i because they are used to it from c.

EDIT: This is an answer for JS if you want history of pre and post increment searc C/C++ pre/post increment. Or see comments on @Orvev's answer.

Paz answered 27/4, 2015 at 1:24 Comment(2)
This is the answer I was looking for, and you stated it the first time without me having to comment. As such, I will mark this answer as correct as soon as I am able to.Fallfish
@Fallfish Strange reason for marking the less complete answer as correct.Novelty
N
13

The difference is that i++ returns the value of i before incrementing and ++i the value of i after incrementing. There is no difference if you ignore the return value, e.g. in:

for (var i = 0; i < 10; i++) {

}

The habit of using ++i over i++ comes from C, where people were worried that storing the old value for i in i++ would incur a performance penalty.

Novelty answered 27/4, 2015 at 1:27 Comment(2)
Yes, I know the difference between pre- and post-incrementation. That was not the question. I'm more asking where the ++i convention comes from.Fallfish
@Fallfish Added that to the answer.Novelty
S
7

Way back in the day (we're talking IE6/7 here!), I recall benchmarking both forms and found that there was a small performance improvement with ++i instead of i++. My (unproven) theory was that a non-optimizing JS engine had to do a tiny bit more work in the i++ case: it had to save the previous value in case it would be used - and being a non-optimizing engine it didn't realize that the value would in fact not be used and didn't need to be saved.

However, with modern browsers there is no significant difference. If anything, i++ seems to be a tiny bit faster in many browsers.

Here are some tests of a variety of different loops:

http://jsperf.com/mikes-loops/5

Look for the results for "Traditional Loop" (this uses ++i) and "Traditional Loop with i++".

Regarding JSLint's requirement that one should never use ++i or i++ but only use i += 1 instead, that is simply insane and over-controlling, like so many other things in JSLint.

Personally I recommend JSHint instead. It is less dogmatic, much more practical, and easier to customize to your own style.

Scutari answered 27/4, 2015 at 1:45 Comment(1)
This is indeed a great one :) I did not know this one.Limiting
P
5

In JS and PHP it does not make any difference, I think even in Java it does not make any difference but in pure c when compiler is not optimizing code it does, and that is why a lot of people use ++i because they are used to it from c.

EDIT: This is an answer for JS if you want history of pre and post increment searc C/C++ pre/post increment. Or see comments on @Orvev's answer.

Paz answered 27/4, 2015 at 1:24 Comment(2)
This is the answer I was looking for, and you stated it the first time without me having to comment. As such, I will mark this answer as correct as soon as I am able to.Fallfish
@Fallfish Strange reason for marking the less complete answer as correct.Novelty
I
2

There is a difference, however not when used in a for loop.

In an expression, i++ evaluates to the previous value of i, and then i is incremented. ++i increments first, and evaluates then.

For this reason, some programmers prefer to write ++i in their for-loops — either because they're used to it, or because they feel it is "more right" somehow.

edit: More probable is the solution Overv proposed: a relict from C.

Isobelisocheim answered 27/4, 2015 at 1:26 Comment(4)
OP obviously knows that, given that he tagged the post with post-increment and pre-increment. :)Photomicroscope
Yes, I know the different between pre- and post-incrementing a variable, but I was specifically asking about a for loop. Also, I know that there is no difference, I simply want to know why people tend to do ++i. Thanks.Fallfish
Ah, sorry. I think it's just because they're used to it because they usually pre-increment, or because they feel it is "more right". Should I delete my answer?Isobelisocheim
No need to delete your answer. I'm more just wondering if there are certain other languages in which ++i is more common, thus programmers coming from those languages to JS prefer that notation or something. Just curious, more than anything.Fallfish

© 2022 - 2024 — McMap. All rights reserved.