Does JS minifiers treat object names properties independently of object and their usage contexts?

78 Views Asked by At

Am I right saying that the following rule works for any/almost-any JS minifier: The property names of all objects of your program are treated as a single set of strings which have no relation to the objects from which they are taken?

Example: consider you have two different objects:

var one = {hello : 111, world : 222};
var two = {hello : 900, sobak : 800};

In your program that objects never "mix": never looked by the same code branches: one.hello does not make the same sense as two.hello. These are different object with completely different meaning of their properties (example: one.hello might mean current temperature and two.hello is water level). They both are different classes.

Can we say that Google Closure Compiler or UglifyJS or other similar machine will translate these two objects to something like:

var A = {a : 111, b : 222};
var Z = {a : 900, c : 800};

Always ASSUMING that both hello properties mean the same thing? That way assigning a as a replacement of any hello property anywhere in code just because "hello" == "hello"?

So, my question is: does "replacement name allocation" (the process of searching minified version for some symbol) does not consider objects usage, but rather consider entire set of names of properties of all your objects with almost no relation to objects? I know: "it more complex than you think", but is it good idea to have such a rule of thumb while dealing with minifiers?

1

There are 1 best solutions below

0
John On

The Closure Compiler can distinguish between two properties with the same name using type information, however this is a whole program analysis. The core property renaming algorithm however does the renaming based purely by name without context and this is applied if type based renaming is disabled, or the type information is insufficient or the type usage prevents the fine reuse of names.