Reputation: 946
I have source codes of some javascript projects and I want to know which of them are compiled using Google's Closure compiler.
Upvotes: 0
Views: 189
Reputation: 5468
There are a couple of quirks that you can look for that may be unique to the Closure Compiler:
(1) the closure compiler rewrites all "while" loops as "for" loops. So if you see a "while" loop, it isn't optimized by the Closure Compiler (it might still be processed by the Closure Compiler, but it hasn't been optimized)
(2) using the default optimization options, the Closure Compiler breaks lines at about 500 characters. If the average line length is significantly less than that or greater than that, it may not be optimized by the compiler.
(3) some constants, the compiler rewrites "undefined", "true" and "false" are rewritten to "void 0" "!0" and "!1" respectively, if you see the others it is NOT optimized by the Closure Compiler (but doesn't tell you that it has been).
Other characteristics may exist but it would depend on the version of the compiler you are using.
Upvotes: 3
Reputation: 7194
I don't think there is a reliable way to know if source code was compressed using Google Closure Compiler. However, as a general test you might try to re-compress the code and compare the before and after result. This assumes simple optimizations were used.
Run the example below and the result returns oiginalSize = 26 and compressedSize = 21 because extra spaces were removed.
See Closure Compiler Service API Reference for more information on using the service. Good luck!
function compress() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://closure-compiler.appspot.com/compile', false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(
'compilation_level=SIMPLE_OPTIMIZATIONS&' +
'output_info=errors&' +
'output_info=statistics&' +
// 'output_info=compiled_code&' +
'output_format=json&' +
'js_code=' + encodeURIComponent( document.getElementById('input1').value )
);
document.getElementById('output1').value = xhr.responseText;
}
textarea { width:100%; height:2em; padding:0.5em; border:1px black solid; margin-bottom:2em; }
Paste your code and click button<p>
<button onclick="compress()">Compress</button>
<textarea id="input1">
alert( "hello world" );
</textarea>
output:
<textarea id="output1"></textarea>
Upvotes: 0