Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a minifier do this? (....and is it a good idea?)

I have a JavaScript application that generates a significant amount of DOM elements. It means that I often use document.createElement("tagname") in my script.

I am thinking about using this simple function:

function c(e) {return document.createElement(e);}

I would keep writing my code in JavaScript (or maybe CoffeScript), and use the full document.createElement method in the code for readability. But I would expect that when I "compile" or minify the code, all the instances of document.createElement are replaced with the c function.

I would do the same for other methods: document.getElementById, etc. I am hoping that I can shorten my code by 10 to 20% with this technique.

Are there minifiers or compilers that can do this? And does it make sense in the first place?

like image 727
Christophe Avatar asked Nov 29 '11 20:11

Christophe


1 Answers

I don't think it will gain you that much. The uncompressed js file might be quite a bit smaller, but compression should deal with with such a repetitive string. So I expect the gain on a compressed(http gzip compression) javascript file to be rather small.

like image 176
CodesInChaos Avatar answered Oct 14 '22 02:10

CodesInChaos