5

I admit that I asked a question about why Closure Compiler does not shorten certain code which looks shortenable at first sight a few days ago already, but that reason is not applicable in this case and I'm not really sure why it isn't shortened here.

What code I have is:

var a = 0;
function b() {
    return a++ >= 3;
}

Now there is pre-incrementing and post-incrementing. The difference is the return value - a++ returns a and then increments it, ++a first increments a and then returns it.

What this comes down to is that my code could be shortened to (ignoring whitespace removal):

var a = 0;
function b() {
    return ++a > 3;
}

However, Closure Compiler does not seem to alter (or recognise) this.

My question therefore is: what side effects could ++a > have when used instead of a++ >=?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • Why would you want it to do that? I can't see how it will improve performance in any way. – Håvard Apr 09 '11 at 19:15
  • Not because of performance, but because of code length. Closure Compiler is there to shorten code by removing whitespace and a lot more, so `a++>=3` could be shortened to `++a>3`. Not very exciting but I was just wondering. – pimvdb Apr 09 '11 at 19:18
  • 3
    you need to meet 2 conditions and gain is very little so they probably didn't even waste their time or are focused on more important things... – fazo Apr 09 '11 at 19:28
  • That could be just the case, but is it correct that they both expressions are equal to each other? – pimvdb Apr 09 '11 at 19:49

3 Answers3

7

There is a particular edge-case for this construct (but not for 3).

It occurs because JavaScript stores numbers as IEEE-754 float-point 64-bit doubles and "only" has a guaranteed "exact" integer-representation up to 2^53 (although implementations may have lee-way to have a higher range, I do not know).

This is on Firefox 4:

a = 2e53
a++ >= 2e53 // true

a = 2e53
++a > 2e53 // false

Real question is what realized gain would such a very particular transformation have? :-0

Happy coding.

  • In Firebug, Firefox 3.6 `(Number.MAX_VALUE + 1) === Number.MAX_VALUE` is `true` – Wayne Apr 09 '11 at 20:13
  • In Chrome too, however `Number.MAX_VALUE + 1e308 === Infinity`. Also Chrome seems to go up to `e+308`. – pimvdb Apr 09 '11 at 20:56
2

It’s safe to apply this size-optimisation if the right-operand (3 in your example) is a constant integer in the range [-252, 252]. In any other case (for example, if the right-operand is fractional or very large), it is not safe.

I would imagine that Closure does not implement this optimisation because:

  • it requires a lot of checking to ensure that the optimisation is safe,
  • it only applies in very specific circumstances that probably don’t come up very often, and
  • It only saves a single character, which hardly seems worth the bother.
Daniel Cassidy
  • 24,676
  • 5
  • 41
  • 54
1

Why not check all of the edge conditions yourself?

function b(a) {
    return a++ >= 3;
}

function b2(a) {
    return ++a > 3;
}

console.log(b(2) === b2(2))
console.log(b(3) === b2(3))
console.log(b(4) === b2(4))

The output is true in each case.

Wayne
  • 59,728
  • 15
  • 131
  • 126