3 ms·
I remember finding a similar thing while working on minifying JavaScript. If you have code like func("hello world"); func("hello world"); Then naively it see
by AshleysBrain 3y ago
I remember finding a similar thing while working on minifying JavaScript. If you have code like
func("hello world");
func("hello world");
Then naively it seems that can be optimized to this:
let s = "hello world";
func(s);
func(s);
However once compressed, the second result is usually larger! It's basically because the second example adds the `let s =` part which is new unique content it has to store.
So if you want to minify JavaScript to a shorter version uncompressed, it's good to transform the first to the second. However if you want to minify JavaScript to the smallest compressed size, it's actually better to do the reverse transform and turn the second case in to the first! But then you get in to tradeoffs with parse time with a longer input, especially with very long strings - so I'm not sure any of today's minifiers actually do that. (Edit - turns out Closure Compiler does: https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it-do-that https://github.com/google/closure-compiler/wiki/FAQ#closure-...)
- astrobe_ 3y agoProgramming and compression are related, see Kolmogorov complexity [1]. It's then no surprise that when you do the "compression" yourself, it is almost like your are compressing twice, which often results in inflation because of the compression dictionary. [1] https://en.wikipedia.org/wiki/Kolmogorov_complexity https://en.wikipedia.org/wiki/Kolmogorov_complexity