How can I estimate the size of my gzipped script?
Asked Answered
R

9

94

How can I estimate the size of my JavaScript file after it is gzipped? Are there online tools for this? Or is it similar to using winzip for example?

Rucksack answered 27/2, 2012 at 16:29 Comment(0)
I
56

http://closure-compiler.appspot.com/home lets you paste in code, and it will give you compression ratios for a particular file before and after GZIP.

Original Size:    90 bytes (100 bytes gzipped)
Compiled Size:    55 bytes (68 bytes gzipped)
Saved 38.89% off the original size (32.00% off the gzipped size)

You can use the pretty-print and white-space only options to estimate the compression of non-minified content.

If you need an estimate:

  • Start with 100 JS files that have gone through the same minification pipeline.
  • For each file, compute the ratio in sizes between gzip -c "$f" | wc -c and wc -c "$f"
  • The average of those ratios is an approximation of the compression you should expect for a similar JS file.

Cygwin contains command line implementations of gzip and wc for Windows.

Inigo answered 27/2, 2012 at 16:43 Comment(1)
thx! I'll try Cygwin, but for now the closure compiler gave me the answer I needed.Rucksack
V
136

If you're on unix - gzip -c filename.min.js | wc -c will give you a byte count of the gzipped file

Volute answered 27/2, 2012 at 16:34 Comment(3)
@Rucksack : gzip is available on Windows through Cygwin or UnxUtils.Discontinuation
Thanks, this is also useful to know raw unzipped byte size by doing gunzip -c myfile | wc -c or zcat myfile | wc -cLarder
To get the un-gzipped size of an un-gzipped file: cat filename.js | wc -c.Eponymous
I
56

http://closure-compiler.appspot.com/home lets you paste in code, and it will give you compression ratios for a particular file before and after GZIP.

Original Size:    90 bytes (100 bytes gzipped)
Compiled Size:    55 bytes (68 bytes gzipped)
Saved 38.89% off the original size (32.00% off the gzipped size)

You can use the pretty-print and white-space only options to estimate the compression of non-minified content.

If you need an estimate:

  • Start with 100 JS files that have gone through the same minification pipeline.
  • For each file, compute the ratio in sizes between gzip -c "$f" | wc -c and wc -c "$f"
  • The average of those ratios is an approximation of the compression you should expect for a similar JS file.

Cygwin contains command line implementations of gzip and wc for Windows.

Inigo answered 27/2, 2012 at 16:43 Comment(1)
thx! I'll try Cygwin, but for now the closure compiler gave me the answer I needed.Rucksack
W
16

Directly from the terminal,

gzip -9 -c path/to/file.js | wc -c | numfmt --to=iec-i --suffix=B --padding=10

If you need the original size for comprison,

cat path/to/file.js | wc -c | numfmt --to=iec-i --suffix=B --padding=10

To get it programatically there are utilities like gzip-size. It's a node package but you can install it globally as a general tool.

Whirly answered 10/6, 2015 at 8:11 Comment(0)
C
9

7-zip supports compressing to the GZIP format.

I often use this to approximate and compare file sizes.

When creating an archive, look for Archive Format, and gzip is the 3rd option.

Update:

In the comments, we discussed that there might be a difference between 7-zip's GZIP compression, versus an actual server's GZIP compression. So, I compared using just the homepage of http://www.google.com/.
Google's GZIP'd payload was 36,678 bytes. 7-zip, with "gzip Normal" setting, was 35,559 (3% smaller). With "gzip Fastest" setting, it was 37,673 (3% larger).

So, long story short: 7-zip had results that were about 97% accurate.

Cordellcorder answered 18/10, 2013 at 22:3 Comment(7)
Sorry, 7-zip can make *smaller archives than most CLI GZIP packers. Great if you want the smallest, bad if you want a precise byte count for what most web servers will make on the fly (which I assume is the intended scenario?Bowser
@Bowser Thanks for the info ... can you back it up with any documentation? 7-zip has a lot of options for gzip, maybe some of them could be configured to match CLI GZIP.Cordellcorder
This is the best test I can Goggle right now. I made a small gzip comparison with gzip vs 7zip vs zopfli a week ago; I'll have to add more tests & formatting which will add time. In the mean while, why don't you provide such tests to prove your point please? :)Bowser
@Bowser Oh, I think you're confused. Maybe I need to re-word my answer. 7-Zip (the application) supports multiple compression formats, including GZIP. I'm not talking about 7z (the compression format), which is undoubtedly better than gzip.Cordellcorder
I understand; I've used 7zip to produce gzip files. .7z won't open in browsers :) However, I always use the Ultra level, 258 (max) word size. So you can be correct that 7zip to size what servers' gzip will produce. I am truly interested in knowing that settings. Do all servers use the same gzip config?Bowser
zopfli.googlecode.com/files/Data_compression_using_Zopfli.pdf by Jyrki Alakuijala, Ph.D. and Lode Vandevenne, M.Sc. (Google Inc.) tested gzip ­9 vs 7­zip ­mm=Deflate ­mx=9. 7zip was constantly smaller in varying degrees. If the range of variance was smaller, I would recommend it (I'm a 7-zip fanboi myself).Bowser
Alright, I updated my answer with a quick test. I tested google.com, and found that 7-zip was 3% smaller. For most people, this ought to be acceptable.Cordellcorder
C
7

http://refresh-sf.com/ will give you minification and gzip ratios & sizes.

Cymbre answered 16/7, 2014 at 22:28 Comment(0)
A
5

With node core, you can use the length of the zlib.gzip buffer to determine the gzip size:

const fs = require('fs');
const util = require('util');
const zlib = require('zlib');
const readFile = util.promisify(fs.readFile);
const gzip = util.promisify(zlib.gzip);

const getGzipSize = filePath => readFile(filePath)
  .then(gzip)
  .then(x => x.length);
Astrodynamics answered 19/8, 2019 at 12:45 Comment(1)
Thanks, this is what I was seeking, to use it with the github script action. Also there are the sync api available: const gzipSizeInKb = Math.round((zlib.gzipSync(content).length / 1024) * 100) / 100;Carpic
I
3

If you want to compare uncompressed, gzipped, and brotli'ed file sizes for the whole folder: (assuming you want to filter *.js):

for file in *.js; do printf "$file\t$(cat $file | wc -c)\t$(gzip -kc7 $file | wc -c)\t$(brotli --in $file --out temp.br --force --quality 11 && cat temp.br | wc -c)\n"; done | tee filesizes.log

Sample output (tab-separated so you can copy to a spreadsheet):

foo.js 39035   10150   8982
bar.js 217000  68978   56337
Index answered 20/12, 2018 at 14:44 Comment(1)
Removed usage of unnecessary temp file, and saving of output to filesizes.log. Added commas to output to improve readability. Bumped gzip compression level to 9 to match the already maxed-out brotli compression level of 11. for file in *.js; do printf "$file\t$(cat $file | wc -c | xargs printf "%'d\n")\t$(gzip -9c $file | wc -c | xargs printf "%'d\n")\t$(brotli -cZ $file | wc -c | xargs printf "%'d\n")\n"; done | teeIrreligion
B
0

You can install gzip itself on Windows via Gow (Gnu On Windows).

Bowser answered 28/3, 2015 at 13:38 Comment(0)
O
0

If you're working in VSCode, then I can recommend this extension: https://marketplace.visualstudio.com/items?itemName=mkxml.vscode-filesize

enter image description here

Oid answered 14/6, 2023 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.