Moritz Roessler
Moritz Roessler

Reputation: 8611

do the parantheses matter?

Given this JSPerf test

Why is this faster

var x;var i = 1E4;var j = 1E4;
for (; i-- > -1;) {
  x = -~x;
}
for (; j-- > -1;) {
  x = ~ - x;
}

Than this ?

var x;var i = 1E4;var j = 1E4;
for (; j-- > -1;) {
  x = -1 * ~x;
}
for (; j-- > -1;) {
  x = ~ (-1 * x);
}

Is the second version better optimizable, or what is the reason?

Upvotes: 2

Views: 125

Answers (2)

osbomb
osbomb

Reputation: 34

In the second code sample, you use j as the iterator in both loops.

Upvotes: 1

imreal
imreal

Reputation: 10358

I believe that in the second test you are either forgetting to reset j to 1E4, or you meant to use an i in one of the loops. See this test:

http://jsperf.com/bit-increment/2

I added a snippet using i in the first loop and j in the second and it performs just as fast as the first test.

Upvotes: 1

Related Questions