Will any JS minifiers replace common property names with strings?

190 Views Asked by At

Will any JS minifiers (uglify, closure, etc) go through the step of adding a variable for long property names? I've tried both and can't find flags to do this, but just wondering if anyone knows something I don't :)

e.g.:

obj.longPropertyName = 42;
obj.longPropertyName++;
obj.longPropertyName++;
obj.longPropertyName++;

could be minified to:

var a='longPropertyName';
obj[a]=42;
obj[a]++;
obj[a]++;
obj[a]++;

[edit] To be clear, closure will do this (or rather it will reduce to obj.a), but it won't with default props like window.addEventListener

1

There are 1 best solutions below

1
On

Yes, some minifier have the additional flag to obfuscate function- and property-names. Mostly, those obfuscations lead to very short property names (but not every time).

EDIT: The YUI compressor for example will do this by default, if you don't pass the --nomunge argument to it.