Forked from bendemboski/controllers.application.js
Last active
April 15, 2018 17:49
-
-
Save nkgm/6b0f6e3f66ddf878b8848ec1f223b9bf to your computer and use it in GitHub Desktop.
Optimizing computed property recalculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
// A computed property chain of property(fullName) <- computed(firstName) <- computed(greeting) will | |
// always recalculate computed(firstName) and computed(greeting) when property(fullName) changes. | |
// This means that computed(greeting) will be recalculated even when computed(firstName) didn't change | |
// as a result of property(fullName)'s change. If you find yourself stuck in an "unnecessary expensive | |
// updates" situation, here is something you can do to optimize: | |
export default Ember.Controller.extend({ | |
appName: 'Optimizing computed property recalculation', | |
fullName: '', | |
// converting computed(firstName) to an observer-derived property. this has the disadvantage of | |
// being calculated eagerly as property(fullName) changes, but has the benefit that all downstream | |
// computed properties will not go through unnecessary invalidation when property(fullName) changes. | |
firstName: '', | |
firstNameObserver: Ember.observer('fullName', function() { | |
this.set('firstName', this.get('fullName').split(' ')[0]); | |
}).on('init'), | |
greeting: Ember.computed('firstName', function() { | |
this.incrementProperty('count'); | |
return `Hello ${this.get('firstName')}`; | |
}), | |
count: 0, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.10.6", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.9.0", | |
"ember-data": "2.0.1", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment