From 7251a3acc219bf87a009c79abf79fc0e732a4f1f Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Tue, 30 Oct 2018 18:01:08 -0700 Subject: [PATCH] memoize behavior method lists for fasting runtime calling. --- lib/legacy/class.html | 148 +++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 73 deletions(-) diff --git a/lib/legacy/class.html b/lib/legacy/class.html index 0872775db5..68e84755d6 100644 --- a/lib/legacy/class.html +++ b/lib/legacy/class.html @@ -14,7 +14,7 @@ 'use strict'; - let metaProps = { + const metaProps = { attached: true, detached: true, ready: true, @@ -22,15 +22,22 @@ beforeRegister: true, registered: true, attributeChanged: true, - // meta objects - behaviors: true }; + const noBehaviorCopyProps = Object.assign({ + behaviors: true + }, metaProps); + + const memoizedProps = Object.assign({ + listeners: true, + hostAttributes: true + }, metaProps); + function copyProperties(source, target) { for (let p in source) { - // NOTE: cannot copy `metaProps` methods onto prototype at least because + // NOTE: cannot copy `noBehaviorCopyProps` methods onto prototype at least because // `super.ready` must be called and is not included in the user fn. - if (!(p in metaProps)) { + if (!(p in noBehaviorCopyProps)) { let pd = Object.getOwnPropertyDescriptor(source, p); if (pd) { Object.defineProperty(target, p, pd); @@ -38,8 +45,7 @@ } } } - - // TODO(sorvell): this breaks `Polymer.mixinBehaviors`; should fix via factoring + /** * Applies a "legacy" behavior or array of behaviors to the provided class. * @@ -78,12 +84,10 @@ let superBehaviors = klass.prototype.behaviors; // get flattened, deduped list of behaviors *not* already on super class behaviors = flattenBehaviors(behaviors, null, superBehaviors); - // mixin new behaviors - // klass = _applyBehaviors(behaviors, klass); if (superBehaviors) { behaviors = superBehaviors.concat(behaviors); } - // Set behaviors on prototype for BC... + // Set behaviors on prototype klass.prototype.behaviors = behaviors; return klass; } @@ -118,16 +122,24 @@ // If lifecycle is called (super then me), order is // (1) C.created, (2) A.created, (3) B.created, (4) element.created // (again same as 1.x) - function _applyBehaviors(behaviors, klass) { + function copyBehaviorProperties(behaviors, klass) { + const meta = {}; if (behaviors) { + klass.prototype.__behaviorMetaProps = meta; for (let i=0; i= 0; i--) { - b = this.behaviors[i]; - if (b.hostAttributes) { - for (let a in b.hostAttributes) { - this._ensureAttribute(a, b.hostAttributes[a]); + const list = this.__behaviorMetaProps.hostAttributes; + if (list) { + for (let i=list.length-1; i >= 0; i--) { + const hostAttributes = list[i]; + for (let a in hostAttributes) { + this._ensureAttribute(a, hostAttributes[a]); } - } } } } @@ -359,12 +371,10 @@ */ ready() { super.ready(); - if (this.behaviors) { - for (let i=0, b; i < this.behaviors.length; i++) { - b = this.behaviors[i]; - if (b.ready) { - b.ready.call(this); - } + let list = this.__behaviorMetaProps.ready; + if (list) { + for (let i=0; i < list.length; i++) { + list[i].call(this); } } if (info.ready) { @@ -376,12 +386,10 @@ * @return {void} */ attached() { - if (this.behaviors) { - for (let i=0, b; i < this.behaviors.length; i++) { - b = this.behaviors[i]; - if (b.attached) { - b.attached.call(this); - } + let list = this.__behaviorMetaProps.attached; + if (list) { + for (let i=0; i < list.length; i++) { + list[i].call(this); } } if (info.attached) { @@ -393,12 +401,10 @@ * @return {void} */ detached() { - if (this.behaviors) { - for (let i=0, b; i < this.behaviors.length; i++) { - b = this.behaviors[i]; - if (b.detached) { - b.detached.call(this); - } + let list = this.__behaviorMetaProps.detached; + if (list) { + for (let i=0; i < list.length; i++) { + list[i].call(this); } } if (info.detached) { @@ -416,12 +422,10 @@ * @return {void} */ attributeChanged(name, old, value) { - if (this.behaviors) { - for (let i=0, b; i < this.behaviors.length; i++) { - b = this.behaviors[i]; - if (b.attributeChanged) { - b.attributeChanged.call(this, name, old, value); - } + let list = this.__behaviorMetaProps.attributeChanged; + if (list) { + for (let i=0; i < list.length; i++) { + list[i].call(this, name, old, value); } } if (info.attributeChanged) { @@ -432,8 +436,6 @@ PolymerGenerated.generatedFrom = info; - // copyProperties(info, PolymerGenerated.prototype); - return PolymerGenerated; }