From f63a07f1397c4a0a74b0e985e6bfd813fb261642 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 14 Aug 2017 12:35:45 -0400 Subject: [PATCH] we can now do changed.foo instead of "foo" in changed - is faster and smaller --- src/generators/dom/index.ts | 4 +--- src/generators/dom/visitors/Component/Binding.ts | 2 +- src/generators/dom/visitors/Component/Component.ts | 6 ++---- src/generators/dom/visitors/EachBlock.ts | 2 +- src/generators/dom/visitors/Element/Attribute.ts | 2 +- src/generators/dom/visitors/shared/Tag.ts | 2 +- src/shared/index.js | 2 +- .../expected-bundle.js | 4 ++-- .../collapses-text-around-comments/expected.js | 2 +- .../samples/computed-collapsed-if/expected-bundle.js | 4 ++-- test/js/samples/computed-collapsed-if/expected.js | 2 +- test/js/samples/css-media-query/expected-bundle.js | 2 +- .../each-block-changed-check/expected-bundle.js | 12 ++++++------ test/js/samples/each-block-changed-check/expected.js | 10 +++++----- .../samples/event-handlers-custom/expected-bundle.js | 2 +- .../js/samples/if-block-no-update/expected-bundle.js | 2 +- test/js/samples/if-block-simple/expected-bundle.js | 2 +- .../non-imported-component/expected-bundle.js | 2 +- .../onrender-onteardown-rewritten/expected-bundle.js | 2 +- test/js/samples/setup-method/expected-bundle.js | 2 +- .../use-elements-as-anchors/expected-bundle.js | 2 +- 21 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 21d155c12f..2513623c49 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -87,9 +87,7 @@ export default function dom( generator.readonly.add(key); - const condition = `isInitial || ${deps.map(dep => - `( '${dep}' in changed )` - ).join(' || ')}`; + const condition = `isInitial || ${deps.map(dep => `changed.${dep}`).join(' || ')}`; const statement = `if ( @differs( ( state.${key} = @template.computed.${key}( ${deps .map(dep => `state.${dep}`) diff --git a/src/generators/dom/visitors/Component/Binding.ts b/src/generators/dom/visitors/Component/Binding.ts index e87ab0bff1..75cdd637e3 100644 --- a/src/generators/dom/visitors/Component/Binding.ts +++ b/src/generators/dom/visitors/Component/Binding.ts @@ -89,7 +89,7 @@ export default function visitBinding( local.update.addBlock(deindent` if ( !${updating} && ${dependencies - .map(dependency => `'${dependency}' in changed`) + .map(dependency => `changed.${dependency}`) .join(' || ')} ) { ${updating} = true; ${local.name}._set({ ${attribute.name}: ${snippet} }); diff --git a/src/generators/dom/visitors/Component/Component.ts b/src/generators/dom/visitors/Component/Component.ts index 56681ddcf6..0e966cdc0f 100644 --- a/src/generators/dom/visitors/Component/Component.ts +++ b/src/generators/dom/visitors/Component/Component.ts @@ -195,10 +195,8 @@ export default function visitComponent( if (attribute.dependencies.length) { return deindent` if ( ${attribute.dependencies - .map(dependency => `'${dependency}' in changed`) - .join( - '||' - )} ) ${name}_changes.${attribute.name} = ${attribute.value}; + .map(dependency => `changed.${dependency}`) + .join(' || ')} ) ${name}_changes.${attribute.name} = ${attribute.value}; `; } diff --git a/src/generators/dom/visitors/EachBlock.ts b/src/generators/dom/visitors/EachBlock.ts index f6b2a07210..1c8a9c9346 100644 --- a/src/generators/dom/visitors/EachBlock.ts +++ b/src/generators/dom/visitors/EachBlock.ts @@ -409,7 +409,7 @@ function unkeyed( // TODO do this for keyed blocks as well const condition = Array.from(allDependencies) - .map(dependency => `'${dependency}' in changed`) + .map(dependency => `changed.${dependency}`) .join(' || '); const parentNode = state.parentNode || `${anchor}.parentNode`; diff --git a/src/generators/dom/visitors/Element/Attribute.ts b/src/generators/dom/visitors/Element/Attribute.ts index 2134ad62d0..5ee1afdea7 100644 --- a/src/generators/dom/visitors/Element/Attribute.ts +++ b/src/generators/dom/visitors/Element/Attribute.ts @@ -157,7 +157,7 @@ export default function visitAttribute( const dependencies = Array.from(allDependencies); const changedCheck = ( ( block.hasOutroMethod ? `#outroing || ` : '' ) + - dependencies.map(dependency => `'${dependency}' in changed`).join(' || ') + dependencies.map(dependency => `changed.${dependency}`).join(' || ') ); const updateCachedValue = `${last} !== ( ${last} = ${value} )`; diff --git a/src/generators/dom/visitors/shared/Tag.ts b/src/generators/dom/visitors/shared/Tag.ts index 3d4f3ed211..f15d72a5ba 100644 --- a/src/generators/dom/visitors/shared/Tag.ts +++ b/src/generators/dom/visitors/shared/Tag.ts @@ -30,7 +30,7 @@ export default function visitTag( if (dependencies.length || hasChangeableIndex) { const changedCheck = ( ( block.hasOutroMethod ? `#outroing || ` : '' ) + - dependencies.map(dependency => `'${dependency}' in changed`).join(' || ') + dependencies.map(dependency => `changed.${dependency}`).join(' || ') ); const updateCachedValue = `${value} !== ( ${value} = ${snippet} )`; diff --git a/src/shared/index.js b/src/shared/index.js index 0ebd390afb..901fbfdb3b 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -28,7 +28,7 @@ export function differs(a, b) { export function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index ee1601cc81..10a7a08930 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -54,7 +54,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; @@ -204,7 +204,7 @@ function create_main_fragment ( state, component ) { }, update: function ( changed, state ) { - if ( 'foo' in changed ) { + if ( changed.foo ) { text.data = state.foo; } }, diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index f773a9eac1..60a7180f1d 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -39,7 +39,7 @@ function create_main_fragment ( state, component ) { }, update: function ( changed, state ) { - if ( 'foo' in changed ) { + if ( changed.foo ) { text.data = state.foo; } }, diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index 112d21f8d8..20fe6ef0fa 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -30,7 +30,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; @@ -191,7 +191,7 @@ function SvelteComponent ( options ) { assign( SvelteComponent.prototype, proto ); SvelteComponent.prototype._recompute = function _recompute ( changed, state, oldState, isInitial ) { - if ( isInitial || ( 'x' in changed ) ) { + if ( isInitial || changed.x ) { if ( differs( ( state.a = template.computed.a( state.x ) ), oldState.a ) ) changed.a = true; if ( differs( ( state.b = template.computed.b( state.x ) ), oldState.b ) ) changed.b = true; } diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index edfca61b86..6318b3b5cf 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -50,7 +50,7 @@ function SvelteComponent ( options ) { assign( SvelteComponent.prototype, proto ); SvelteComponent.prototype._recompute = function _recompute ( changed, state, oldState, isInitial ) { - if ( isInitial || ( 'x' in changed ) ) { + if ( isInitial || changed.x ) { if ( differs( ( state.a = template.computed.a( state.x ) ), oldState.a ) ) changed.a = true; if ( differs( ( state.b = template.computed.b( state.x ) ), oldState.b ) ) changed.b = true; } diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index ee643bae65..868ca03e71 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -50,7 +50,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 15410339ef..2d14d12151 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -63,7 +63,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; @@ -209,7 +209,7 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { var each_block_value = state.comments; - if ( 'comments' in changed || 'elapsed' in changed || 'time' in changed ) { + if ( changed.comments || changed.elapsed || changed.time ) { for ( var i = 0; i < each_block_value.length; i += 1 ) { if ( each_block_iterations[i] ) { each_block_iterations[i].update( changed, state, each_block_value, each_block_value[i], i ); @@ -227,7 +227,7 @@ function create_main_fragment ( state, component ) { each_block_iterations.length = each_block_value.length; } - if ( 'foo' in changed ) { + if ( changed.foo ) { text_1.data = state.foo; } }, @@ -289,15 +289,15 @@ function create_each_block ( state, each_block_value, comment, i, component ) { }, update: function ( changed, state, each_block_value, comment, i ) { - if ( ( 'comments' in changed ) && text_2_value !== ( text_2_value = comment.author ) ) { + if ( ( changed.comments ) && text_2_value !== ( text_2_value = comment.author ) ) { text_2.data = text_2_value; } - if ( ( 'elapsed' in changed || 'comments' in changed || 'time' in changed ) && text_4_value !== ( text_4_value = state.elapsed(comment.time, state.time) ) ) { + if ( ( changed.elapsed || changed.comments || changed.time ) && text_4_value !== ( text_4_value = state.elapsed(comment.time, state.time) ) ) { text_4.data = text_4_value; } - if ( ( 'comments' in changed ) && raw_value !== ( raw_value = comment.html ) ) { + if ( ( changed.comments ) && raw_value !== ( raw_value = comment.html ) ) { detachBetween( raw_before, raw_after ); raw_before.insertAdjacentHTML( 'afterend', raw_value ); } diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 07feca6020..9bfc875e78 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -35,7 +35,7 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { var each_block_value = state.comments; - if ( 'comments' in changed || 'elapsed' in changed || 'time' in changed ) { + if ( changed.comments || changed.elapsed || changed.time ) { for ( var i = 0; i < each_block_value.length; i += 1 ) { if ( each_block_iterations[i] ) { each_block_iterations[i].update( changed, state, each_block_value, each_block_value[i], i ); @@ -53,7 +53,7 @@ function create_main_fragment ( state, component ) { each_block_iterations.length = each_block_value.length; } - if ( 'foo' in changed ) { + if ( changed.foo ) { text_1.data = state.foo; } }, @@ -115,15 +115,15 @@ function create_each_block ( state, each_block_value, comment, i, component ) { }, update: function ( changed, state, each_block_value, comment, i ) { - if ( ( 'comments' in changed ) && text_2_value !== ( text_2_value = comment.author ) ) { + if ( ( changed.comments ) && text_2_value !== ( text_2_value = comment.author ) ) { text_2.data = text_2_value; } - if ( ( 'elapsed' in changed || 'comments' in changed || 'time' in changed ) && text_4_value !== ( text_4_value = state.elapsed(comment.time, state.time) ) ) { + if ( ( changed.elapsed || changed.comments || changed.time ) && text_4_value !== ( text_4_value = state.elapsed(comment.time, state.time) ) ) { text_4.data = text_4_value; } - if ( ( 'comments' in changed ) && raw_value !== ( raw_value = comment.html ) ) { + if ( ( changed.comments ) && raw_value !== ( raw_value = comment.html ) ) { detachBetween( raw_before, raw_after ); raw_before.insertAdjacentHTML( 'afterend', raw_value ); } diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index dafef990c2..97090f092d 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -50,7 +50,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index f3f7a371be..a36be53f93 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -54,7 +54,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 1ffa205c9c..c16dbcaaa3 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -54,7 +54,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 98c6b12764..09576689bc 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -44,7 +44,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index f06892cbfb..227ac71e7c 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -30,7 +30,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 4421ec1f87..0ecdbafa5b 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -30,7 +30,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key]; diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index 9b64cd8600..33f1b49df3 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -54,7 +54,7 @@ function differs(a, b) { function dispatchObservers(component, group, changed, newState, oldState) { for (var key in group) { - if (!(key in changed)) continue; + if (!changed[key]) continue; var newValue = newState[key]; var oldValue = oldState[key];