0

I just noticed something interesting on most minification libraries.

This is a method from angular.js

function isNumber(value) {return typeof value === 'number';}

As you can see typeof value value is on the left side of the comparison on the source code. But after minification it looks like this.

function Q(a){return"number"===typeof a}

Now the static member is at the left side of the comparison on angular.min.js minified code.

All cases of evaluated value === static comparisons are converted to static === evaluated value.

I just want to learn the reason of it. Is it better for reliability or performance or something else?

Ahmet Can Güven
  • 5,392
  • 4
  • 38
  • 59

2 Answers2

2

It's just shorter. If they left it in the original order, it would need a space after return:

function Q(a){return typeof a==="number"}

But the quote around number is a token delimiter, so no space is needed. The above line is one character longer than the minified version.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Because javascript engine allows return statement to be followed by string without a space. And that simply... saves space.

lukaleli
  • 3,427
  • 3
  • 22
  • 32