Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

improve performance for template repeat over model objects with shared prototype #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,8 @@

var observedSetCache = [];

var hasOwnProperty = Object.prototype.hasOwnProperty;

function newObservedSet() {
var observerCount = 0;
var observers = [];
Expand All @@ -619,15 +621,24 @@
if (!obj)
return;

if (obj === rootObj)
var observeProto = true;
if (obj === rootObj) {
rootObjProps[prop] = true;

// If we find the property on the root object, we can skip observing
// the prototype, which is expensive. This helps a common use case:
// when the user has <template repeat> over items that all share a
// prototype. See https://github.com/Polymer/polymer-dev/issues/101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to handle the case where the own property gets deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, I should at least have a test for that & mention it the comment. It should work because whenever it gets a change record (in callback below), it will rescan everything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that will work since we'll run through this code path in that case again and object will no longer have the own-property -- but a test would be ideal.

observeProto = !hasOwnProperty.call(rootObj, prop);
}

if (objects.indexOf(obj) < 0) {
objects.push(obj);
Object.observe(obj, callback);
}

observe(Object.getPrototypeOf(obj), prop);
if (observeProto)
observe(Object.getPrototypeOf(obj), prop);
}

function allRootObjNonObservedProps(recs) {
Expand Down