Created
January 20, 2016 14:39
-
-
Save peterbe/cf049e4c0c2dcb47238d to your computer and use it in GitHub Desktop.
Comparing Closure Compiler and UglifyJS2 http://www.peterbe.com/plog/advanced-closure-compiler-vs-uglifyjs2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StringIO | |
import gzip | |
import os | |
import subprocess | |
import time | |
def compiler(path): | |
cmd = 'java -jar compiler-latest/compiler.jar -O advanced'.split() | |
cmd.append(path) | |
t0 = time.time() | |
output, error = call(cmd) | |
if not output.strip(): | |
# When you use `-O advanced` | |
cmd = 'java -jar compiler-latest/compiler.jar -O simple'.split() | |
cmd.append(path) | |
output, error = call(cmd) | |
error = 1 | |
t1 = time.time() | |
return output.strip(), t1-t0, error | |
def uglify(path): | |
cmd = ['./node_modules/.bin/uglifyjs'] | |
cmd.append(path) | |
cmd.append('--mangle') | |
cmd.append('--screw-ie8') | |
t0 = time.time() | |
output, error = call(cmd) | |
t1 = time.time() | |
return output.strip(), t1-t0, error | |
def gzip_string(s): | |
out = StringIO.StringIO() | |
with gzip.GzipFile(fileobj=out, mode="w") as f: | |
f.write(s) | |
return out.getvalue() | |
def call(seq): | |
if isinstance(seq, basestring): | |
seq = seq.split() | |
return subprocess.Popen( | |
seq, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT | |
).communicate() | |
def run(*args): | |
paths = args[0].splitlines() | |
import re | |
paths = [x for x in paths if not re.findall('\.[a-f0-9]{12}\.', x)] | |
# import random | |
# random.shuffle(paths) | |
# paths=paths[:3] | |
print len(paths), "FILES" | |
print "PATH".ljust(30), | |
print "SIZE".rjust(10), | |
print "CLOSURE".rjust(10), | |
print "(GZIP)".rjust(10), | |
print "(TIME)".rjust(10), | |
print "UGLIFY".rjust(10), | |
print "(GZIP)".rjust(10), | |
print "(TIME)".rjust(10), | |
before_total = total = utotal = gtotal = ugtotal = 0 | |
ttotal = uttotal = 0.0 | |
for path in paths: | |
result, t, errors = compiler(path) | |
assert result, path | |
uresult, ut, uerrors = uglify(path) | |
assert result, path | |
p = os.path.basename(path) | |
if errors: | |
p += ' *' | |
if uerrors: | |
p += ' ^' | |
print p.ljust(30), | |
before = os.stat(path).st_size | |
print str(before).rjust(10), | |
print str(len(result)).rjust(10), | |
print str(len(gzip_string(result))).rjust(10), | |
print ('%.2fs' % t).rjust(10), | |
print str(len(uresult)).rjust(10), | |
print str(len(gzip_string(uresult))).rjust(10), | |
print ('%.2fs' % ut).rjust(10), | |
before_total += before | |
total += len(result) | |
utotal += len(uresult) | |
gtotal += len(gzip_string(result)) | |
ugtotal += len(gzip_string(uresult)) | |
ttotal += t | |
uttotal += ut | |
print "TOTAL".ljust(30), | |
print str(before_total).rjust(10), | |
print str(total).rjust(10), | |
print str(gtotal).rjust(10), | |
print ('%.2fs' % ttotal).rjust(10), | |
print str(utotal).rjust(10), | |
print str(ugtotal).rjust(10), | |
print ('%.2fs' % uttotal).rjust(10), | |
if __name__ == '__main__': | |
import sys | |
sys.exit(run(sys.stdin.read())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm init
andnpm install --save uglify-js
(or justnpm install -g uglify-js
if you want to be global).js
files and run it something like this:If you can, try to avoid directories such as
node_modules
orbower
.