Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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),
print
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),
print
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),
print
if __name__ == '__main__':
import sys
sys.exit(run(sys.stdin.read()))
@peterbe

This comment has been minimized.

Copy link
Owner Author

commented Jan 20, 2016

  1. Download the compiler from: https://dl.google.com/closure-compiler/compiler-latest.zip (link from https://developers.google.com/closure/compiler/)
  2. run npm init and npm install --save uglify-js (or just npm install -g uglify-js if you want to be global)
  3. Pick some directory that has (recursively) lost of your .js files and run it something like this:
find /path/to/my/project/ | grep '\.js$' | grep -v '\.min\.'  | python compare.py

If you can, try to avoid directories such as node_modules or bower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.