Search code examples
javascriptoperatorsgoogle-closure-compilerprimitive

Why does Closure Compiler favor !0 in lieu of true?


Example:

var formatDate = function(d) {
  return dateFormat(d, "yyyy-mm-dd HH:MM:ss", true);
}

Is compiled to:

var formatDate = function(a) {
  return dateFormat(a, "yyyy-mm-dd HH:MM:ss", !0);
};

!0 feels like a logical operation in place a Javascript primitive, which intuitively seems like it should be more expensive. Is this not the case? Why not?


Solution

  • Gzip compression was found to be better. Closure-compiler uses heuristics to try to produce a file that will gzip to the smallest possible size.

    Note: In some cases this makes the non-gzipped file bigger. In this specific case it's a win for the raw file and the gzip version.