Skip to content

Commit

Permalink
optimize caching CPU utilization dramatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Anderson committed Apr 30, 2015
1 parent cb76798 commit 57303bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
10 changes: 8 additions & 2 deletions dist/rest-model.js
Expand Up @@ -823,7 +823,13 @@ var RestModel = module.exports = Ember.Object.extend({
var cachedModel = utils.findMatching(newModel, self, cachedResponse);

if (cachedModel) {
return cachedModel.setProperties(newModel);
// we only want to update properties that we actually track
var attrs = cachedModel.get('attrs');
var properties = attrs.reduce(function(properties, attr) {
properties[attr] = newModel.get(attr);
return properties;
}, {});
return cachedModel.setProperties(properties);
} else {
return cachedResponse.pushObject(newModel);
}
Expand Down Expand Up @@ -2345,7 +2351,7 @@ exports.findMatching = function(toMatch, klass, data) {
var primaryStorageKey = klass.primaryKeys[0];

return data.find(function(datum) {
return Ember.get(toMatch, primaryStorageKey) === Ember.get(datum, primaryStorageKey);
return toMatch[primaryStorageKey] === datum[primaryStorageKey];
});
};

Expand Down
2 changes: 1 addition & 1 deletion dist/rest-model.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -822,7 +822,13 @@ var RestModel = module.exports = Ember.Object.extend({
var cachedModel = utils.findMatching(newModel, self, cachedResponse);

if (cachedModel) {
return cachedModel.setProperties(newModel);
// we only want to update properties that we actually track
var attrs = cachedModel.get('attrs');
var properties = attrs.reduce(function(properties, attr) {
properties[attr] = newModel.get(attr);
return properties;
}, {});
return cachedModel.setProperties(properties);
} else {
return cachedResponse.pushObject(newModel);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Expand Up @@ -51,7 +51,7 @@ exports.findMatching = function(toMatch, klass, data) {
var primaryStorageKey = klass.primaryKeys[0];

return data.find(function(datum) {
return Ember.get(toMatch, primaryStorageKey) === Ember.get(datum, primaryStorageKey);
return toMatch[primaryStorageKey] === datum[primaryStorageKey];
});
};

Expand Down

0 comments on commit 57303bd

Please sign in to comment.