Are JavaScript Minification Tools Getting Too “Try Hard?”
Note this article was not on my planned schedule. It came up whilst doing work that pays better than writing here, but I just had to get this out there.
In web development JavaScript minification is a quick, dirty, and easy way to squeeze a hair more speed out of your websites. Just strip out unneeded whitespace, brackets, maka a few variable name changes to single letters, and the resultant smaller files can consume half or less the bandwidth, and take less space in the user’s cache. Seems like a no-brainer.
But I just had a client notice something… odd. I had written them a smaller script that was 2k… being it’s for a client I can’t share it but we got these results out of Google’s “Closure Compiler Service”
It made the file… larger? Delta Alpha Foxtrot Uniform Quebec?!?
Now, it’s so small I wouldn’t have bothered minifying it, but this result is strange… I dig in, and closure adds all sorts of crazy code of their own… so I asked myself if I just manually whitespace stripped that 2k file, how big is it?
1.5k / 642 bytes gzipped.
I tried a few other minifiers most of which seem to be based on something called “terser”, and they gave a smaller file size, but did something strange. They deleted an outer {} used for scope…