Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In the minified code there are still a lot of Math.PI, Math.sin, Math.cos… could this not be optimised further with something like s=Math.sin…?


In the original article, the author claims that lots of repeated strings turn out to be beneficial insofar as they make compression (gzip or whatever) more effective. According to him, it doesn't make sense to manually de-redundantize code when the compressor will achieve the same gain or better.


I was about to say "well, it's probably a lot of work to verify that s isn't already a mangled variable name" but then I realized that doing what you suggested in the pre-minified code should work, yeah.

So not only are you right, it's actually super easy. Just do this at the top level of your code:

    const {cos, sin, PI /* etc */} = Math;


You could do that, but it would not compress as well. For example the word const is not use anywhere in the 2k version, while Math. is used about 40 times so it becomes essentially free when compressed.


> For example the word const is not use anywhere in the 2k version, while Math. is used about 40 times so it becomes essentially free when compressed.

Except that the word const does seem to be used in the uncompressed version, so we don't know what happens during compression in the Closure compiler, no? Or is the 2k version pre-Closure compiler different from the uncompressed version?

Either way the only way to know is to try because asymptotically free is all well and good, but without measurement we don't know the actual behavior in practice and we are just making assumptions about how well or how badly things compress and how the mangling process works out.


Ok, I just tried it. Removing every Math. and adding let{cos,sin,PI,min,max,tan,atan2,abs,ceil}=Math; to the top. It worked better then I expected, though it is still 1 byte larger then without. Could potentially be a win in some situations.


Nice! Thanks for trying!

> it is still 1 byte larger then without

That's actually pretty cool, because together with your earlier remark of ~40x occurrences of Math.<whatever> that means we have a ballpark figure for where the tipping point is!


It gets zipped so the duplication goes away. In fact, repetition is deliberate to get better compression.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: