Last active
April 4, 2019 14:32
-
-
Save peterbe/fd51c48050486d4abd0ebb0f0c55409c to your computer and use it in GitHub Desktop.
This file contains 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 hashlib | |
import os | |
import shutil | |
from collections import defaultdict | |
from glob import glob | |
from subprocess import check_call | |
import pyquery | |
def md5(s): | |
return hashlib.md5(s.encode('utf-8')).hexdigest()[:7] | |
shutil.rmtree('downloaded') | |
os.makedirs('downloaded') | |
doc = pyquery.PyQuery('https://developer.mozilla.org/en-US/') | |
counts = defaultdict(list) | |
for svg in doc('svg').items(): | |
name = str(repr(svg))[2:-2] | |
html = svg.outer_html() | |
name += '_' + md5(html) | |
if name not in counts: | |
fn = 'downloaded/' + name + '.svg' | |
with open(fn, 'w') as f: | |
f.write(html) | |
f.write('\n') | |
check_call(("svgo {} -o {}".format(fn, fn.replace('.svg','.min.svg'))).split()) | |
counts[name].append(len(html)) | |
for x in glob('downloaded/*.min.svg'): | |
check_call(("zopfli -i100 {}".format(x)).split()) | |
check_call(("brotli -9 {}".format(x)).split()) | |
def kb(b): | |
return '{:.1f}KB'.format(b / 1024) | |
# print(counts) | |
print("TOTAL BYTES BEFORE ({}): {:,} ({})".format( | |
sum(len(x) for x in counts.values()), | |
sum(sum(x) for x in counts.values()), | |
kb(sum(sum(x) for x in counts.values())) | |
)) | |
after = { | |
x: os.stat(x).st_size | |
for x in glob('downloaded/*.min.svg') | |
} | |
# print(after) | |
print("TOTAL BYTES AFTER ({}): {:,} ({})".format( | |
len(after), | |
sum(after.values()), | |
kb(sum(after.values())) | |
)) | |
gzipped = { | |
x: os.stat(x).st_size | |
for x in glob('downloaded/*.min.svg.gz') | |
} | |
# print(gzipped) | |
print("TOTAL BYTES ZOPFLI ({}): {:,} ({})".format( | |
len(gzipped), | |
sum(gzipped.values()), | |
kb(sum(gzipped.values())) | |
)) | |
brotlied = { | |
x: os.stat(x).st_size | |
for x in glob('downloaded/*.min.svg.br') | |
} | |
# print(brotlied) | |
print("TOTAL BYTES BROTLI ({}): {:,} ({})".format( | |
len(brotlied), | |
sum(brotlied.values()), | |
kb(sum(brotlied.values())) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context; this blog post: https://www.peterbe.com/plog/optimize-inlined-svg-on-developer.mozilla.org