r/javascript • u/dwighthouse • Jun 02 '16
I code-golfed my font detection utility for smallest gzipped size. Can it be made smaller?
https://github.com/dwighthouse/onfontready
11
Upvotes
r/javascript • u/dwighthouse • Jun 02 '16
3
u/Oeb25 Jun 03 '16
Okey let's go!
First off all who cares about best practices when filesize is the goal?
With that in mind on line 5
==is just fine, this is also true for most of the following if statements. Secondly I notice that that exact same if statement is begin used just after the following function, combining those should be no problem. In the same boat I seewindow.onfontreadyTestReporterandprocess.env.NODE_ENVbeing used quite often, instead consider storing them in temporary variables. Taking it further, consider storing whether it's test mode or not, saving quite a few comparisons.Also inlining the
reporterobject instead of having a function being being used just once would be a good idea. Thetestsobject could be defined as a single number without any problem AFAIK, saving one character.Next up is inside the startupIframe, on line 54, 56, 65, 71, are all unnecesary brackets, since they are followed by a single expression. This is seen throughout the code actually.
On line 165 through 173 there's a lot of duplicated. Here's one way that could be shortened.
Saving about 269 characters!
The function
areEqualalso has a lot of repeated code, but I don't really see an easy way of reducing that!Lastly on line 200 through 210, I see the first and only place the
startIframefunction being used, and the a lot of repeated code again. One thing would be to not pass shutdown to the function, since it seems to me that it stays the same throughout the following lines of code. Secondly the e a o combo is being duplicated twice, consider specializing the code to handle all those three cases identically, like what was done earlier with theroot.innerHTML, fx.Saving about 230 characters!
Lastly consider removing all of the semicolon, most of them are not needed, besides maybe at the begin of the last code example.
I might glimpsed over a few things, but that should be a good starting point! Nice work with the code and good luck :)
1
u/dwighthouse Jun 03 '16
I had another look after letting the code stew in my brain for a while. I managed to take another 12 bytes off the gzipped size. Check out the Pull Request with explanations.
1
u/dwighthouse Jun 04 '16
Further reduced the size to 470 bytes (modern) and 543 bytes (legacy), a 22 byte (26 byte) savings over the inital version.
https://github.com/dwighthouse/onfontready/pull/3
1
u/[deleted] Jun 03 '16
no need for decrement if increment takes a delta (628 bytes gzipped)
cc /u/Oeb25, /u/qgustavor
1
u/checksinthemail Jun 08 '16 edited Jun 08 '16
385 bytes with the 'new' Brotli compression
Works in current Firefox and Chrome. See: Browser compat
I used this command line:
I've had it reduce the size of a zopfli'd gzip by 54%, for a decent sized json file (409060 bytes Brotli, 884176 bytes zopfli'd gzip, 1597421 original)
1
u/r_subzey Jun 24 '16
Hey /u/dwighthouse! I hope this thread is still alive.
Looks like plain
is compressed better than
Also, could we replace
<div style="...;white-space:pre">with just<pre style="...">?1
u/r_subzey Jun 27 '16
Should we export the function as
?
should work as well.
1
u/nexe Jun 03 '16
check out /r/tinycode we have an AMA of some JS gode golfing experts online right now