-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PROPOSAL] mangle property #3293
Comments
This sort of subexpression aliasing proposal comes up a lot. The gzip output is larger.
|
Yes, I agree than gzip loves repetitions for better compression. But it could be worth testing this method on big libraries (like angular or react which use a lot of DOM methods) and see what append. PS: that's why I proposed a 'soft' flag which allow developpers to test if the code is smaller or not with this optimization. |
Tested a few aliasing variations in the past. Current optimizations seem to work best for most code post gzip. In addition to But don't let me stop you from creating a PR and proving otherwise. Compare sizes with |
Feature request
Idea to mangle repetitive object properties.
Uglify version (
uglifyjs -V
)3.4.9
Uglify script
Lets assume this default code to test compression:
Repetitive object properties access example
Lets assume a script which inserts 10 text nodes in the DOM, or in a more generic manner, a script which gets many times the same property name from same or different objects.
In this example, the property
appendChild
is called 11 times.The output code is:
The length is 303B
As we can see,
appendChild
is repeated many times.If we use
properties: true
the output becomes:The length is 210B
But the code becomes invalid !
Repetitive object properties access optimization
Lets rewrite the code in such a manner than an object property, is not accessed with dot but with a function instead.
doc.appendChild
becomesappendChild(doc)
.The output is:
The length is 250B
And more important: the code is totally valid !
obj.property1.property2.property3.property4
could be written asproperty4( property3( property2( property1(obj))))
which could potentially compress toa(b(c(d(e))))
Introducing this method to uglify js
The parser could detect repetitive properties access, and convert them to functions to enable stronger compression. I suggest kind of:
properties: true | false | 'none' | 'hard' | 'soft'
.Where
false
map to'none'
,true
map to'hard'
and have the same current behavior. Plus the introduction of'soft'
which tries to convert properties to functions (only if resulting size is smaller).This technique could save a lot of bytes in classes using long property names.
Performances
This is a simple test of performances on chrome 70 :
As we can see, V8 keeps really good performances on this pattern.
PS: I present here a generic idea how to optimize object's properties with functions for compression.
This method would probably requires some adjustments according to the access context: call, set, get ?
The text was updated successfully, but these errors were encountered: