Skip to content

Commit 9dcc388

Browse files
author
Scott J. Miles
committed
optimize iterations by caching keylist for publish and observe maps
1 parent ec74922 commit 9dcc388

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

src/declaration/prototype.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@
6363
// get basal prototype
6464
var base = this.generateBasePrototype(extendee);
6565
// chain observe object
66-
if (prototype.hasOwnProperty('observe') && base.hasOwnProperty('observe')) {
67-
chainObject(prototype.observe, base.observe);
68-
//console.log('observed:', prototype.observe);
66+
if (prototype.hasOwnProperty('observe')) {
67+
if (base.hasOwnProperty('observe')) {
68+
chainObject(prototype.observe, base.observe);
69+
}
70+
// combine name list
71+
prototype._observeNames = Object.keys(prototype.observe).concat(base._observeNames || []);
6972
}
7073
// chain publish object
71-
if (prototype.hasOwnProperty('publish') && base.hasOwnProperty('publish')) {
72-
chainObject(prototype.publish, base.publish);
73-
//console.log('published:', prototype.publish);
74+
if (prototype.hasOwnProperty('publish')) {
75+
if (base.hasOwnProperty('publish')) {
76+
chainObject(prototype.publish, base.publish);
77+
}
78+
// combine name list
79+
prototype._publishNames = Object.keys(prototype.publish).concat(base._publishNames || []);
7480
}
7581
// chain custom api
7682
chainObject(prototype, base);

src/instance/properties.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,32 @@
2121
var properties = {
2222
// set up property observers
2323
observeProperties: function() {
24-
// TODO(sjmiles):
24+
// TODO(sjmiles):
2525
// we observe published properties so we can reflect them to attributes
2626
// ~100% of our team's applications would work without this:
2727
// perhaps we can make it optional somehow
28-
//console.group('[%s]:observeProperties', this.localName);
29-
// add observers as explicitly requested
30-
for (var n in this.observe) {
31-
//console.log('observable:', n);
32-
var m = this.observe[n];
33-
//if (this.publish && this.publish[n]) {
34-
//this.observeBoth(n, m);
35-
//} else {
36-
this.observeProperty(n, m);
37-
//}
28+
//
29+
// add user's observers
30+
var n$ = this._observeNames;
31+
if (n$) {
32+
for (var i=0, l=n$.length, n; (i<l) && (n=n$[i]); i++) {
33+
var m = this.observe[n];
34+
if (this.publish && (this.publish[n] !== undefined)) {
35+
this.observeBoth(n, m);
36+
} else {
37+
this.observeProperty(n, m);
38+
}
39+
}
3840
}
39-
// add observers for left-over published properties
40-
for (var n in this.publish) {
41-
//if (this.observe && !this.observe[n]) {
42-
//console.log('attr-prop:', n);
43-
this.observeAttributeProperty(n);
44-
//}
41+
// add observers for published properties
42+
var n$ = this._publishNames;
43+
if (n$) {
44+
for (var i=0, l=n$.length, n; (i<l) && (n=n$[i]); i++) {
45+
if (!this.observe || (this.observe[n] === undefined)) {
46+
this.observeAttributeProperty(n, this.publish[n]);
47+
}
48+
}
4549
}
46-
//console.groupEnd();
4750
},
4851
_observe: function(name, cb) {
4952
log.watch && console.log(LOG_OBSERVE, this.localName, name);
@@ -62,15 +65,13 @@
6265
invoke.call(self, methodName, [old]);
6366
});
6467
},
65-
/*
6668
observeBoth: function(name, methodName) {
6769
var self = this;
6870
this._observe(name, function(value, old) {
6971
self.relectPropertyToAttribute(name);
7072
invoke.call(self, methodName, [old]);
7173
});
7274
},
73-
*/
7475
bindProperty: function(property, model, path) {
7576
// apply Polymer two-way reference binding
7677
return bindProperties(this, property, model, path);

0 commit comments

Comments
 (0)