optimize style attributes #810
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ref #455. This generates better code for style attributes — this...
...becomes this:
function create_main_fragment ( state, component ) { - var div, div_style_value; + var div; return { create: function () { div = createElement( 'div' ); this.hydrate(); }, hydrate: function ( nodes ) { - div.style.cssText = div_style_value = "font-size: 12px; color: " + ( state.color ) + "; transform: translate(" + ( state.x ) + "px," + ( state.y ) + "px);"; + div.style.setProperty('font-size', "12px"); + div.style.setProperty('color', state.color); + div.style.setProperty('transform', "translate(" + state.x + "px," + state.y + "px)"); }, mount: function ( target, anchor ) { insertNode( div, target, anchor ); }, update: function ( changed, state ) { - if ( ( changed.color || changed.x || changed.y ) && div_style_value !== ( div_style_value = "font-size: 12px; color: " + ( state.color ) + "; transform: translate(" + ( state.x ) + "px," + ( state.y ) + "px);" ) ) { - div.style.cssText = div_style_value; - } + if ( changed.color ) { + div.style.setProperty('color', state.color); + } + + if ( changed.x || changed.y ) { + div.style.setProperty('transform', "translate(" + state.x + "px," + state.y + "px)"); + } }, unmount: function () { detachNode( div ); }, destroy: noop }; }It's more lines but less code, and according to the benchmarks I've done (not to mention common sense) is faster in the vast majority of cases.
Will probably add a
stylehelper to shrink the code down further:TODO: