Introduction of the difference between i + + and + + i in JS for cycle

  • 2021-07-04 18:07:21
  • OfStack

The for loop is usually written like this.


for(var i = 0; i < 20 ; i++){ 
.... 
}

But I think some people write like this


for (var i = 0; i < 20 ; ++i) {
....
}

How does this affect the for loop?

1. It doesn't seem to make any difference in for...

But when it comes to assigning values, there's a difference...


var a = 1;
var b = a++;
var c = 1;
var d = ++c;
console.log(b,d); // 1, 2 

a + + is to apply a first and then self-add

+ + a is self-added first, and then a is applied

2. Probably a habit brought about by C/C + +. It is said that + + i is faster than i + +, but in fact the performance of both is similar in js.

As for the specific details, the self-increase/self-decrease operators in the specification are written as follows:

For the first case-


UnaryExpression : ++ UnaryExpression
Let expr be the result of evaluating UnaryExpression.
Let oldValue be ToNumber(GetValue(expr)).
ReturnIfAbrupt(oldValue).
Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 12.7.5).
Let status be PutValue(expr, newValue).
ReturnIfAbrupt(status).
Return newValue.

Roughly translate 1 below:

1. Make expr equal to the result of the current expression (that is, the part after + +)

2. Make oldValue equal to the result of converting expr into a number

3. Use the same rule as the "+" operator 1 so that newValue = oldValue + 1

4. Assign newValue to expr

5. The entire expression returns newValue

For the latter case--

PostfixExpression : LeftHandSideExpression ++

1.Let lhs be the result of evaluating LeftHandSideExpression.

2.Let oldValue be ToNumber(GetValue(lhs)).

3.ReturnIfAbrupt(oldValue).

4.Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 12.7.5).

5.Let status be PutValue(lhs, newValue).

6.ReturnIfAbrupt(status).

7.Return oldValue.

General translation-

1. Make lhs equal to the result of the current expression (the part before + +)

2. Make oldValue equal to the result of converting lhs into a number

3. Use the same rules as the "+" operator 1 so that newValue = oldValue + 1

4. Assign newValue to lhs

5. The entire expression returns oldValue

As you can see, the difference between the two is only the last step, whether the whole expression returns after adding 1 or before adding 1.

For the for statement:


for(var i = 0; i < n; i++)

The last column is only self-addition, and it doesn't assign this value to anyone, so from this point of view, the + + symbol is one before or after.

3. Actually, it doesn't make much difference. But if you want to pursue that 1 point efficiency problem, + + i will be 1 point more efficient.

It is indeed from std standard library of c + +. When i is iterator of an std container, i is not a number, and i + + will generate one more assignment operation, so + + i has higher performance. Therefore, people who are used to c + + like to use + + i to save money when i is not a numerical value.


Related articles: