I have an ExtJS Panel that displays several bits of data and needs to be updated frequently. For example, a panel that displays current market prices for the DOW, NYSE, NASDAQ, etc.
Considering that the data will be updated frequently (at least every second), is it significantly more efficient to update individual elements for the data fields vs. re-generating all the HTML using an XTemplate each time?
For example, if I don't reapply an XTemplate it might look like this:
update: function(marketData) {
Ext.get('nasdaq-market-price-div-id').update(marketData.nasdaqPrice);
Ext.get('nyse-market-price-div-id').update(marketData.nysePrice);
//Repeat for all fields...
}
Compared to using an XTemplate each time, like this:
update: function(marketData) {
var tpl = new XTemplate('<div>{nasdaqPrice}</di开发者_运维百科v><div>{nysePrice}</div>');
var html = tpl.applyTemplate(marketData);
Ext.get('market-data-div-id').update(html);
}
I like using XTemplate because it allows me to separate the HTML (into a separate file, actually) and helps with code readability/maintainability. However, does anyone know if there's a prohibitive performance hit by reapplying the template each time? I'm guessing there is, but am hoping I can get an authoritative answer from someone "in the know."
Thanks!
I agree with @timdev -- XTemplate should be fine (and is very fast BTW). However, I would not create the XTemplate from scratch every time. I'd do something more like this:
update: function(marketData) {
// assuming that this is a class or some appropriate scope
if(!this.PriceTpl){
this.PriceTpl = new XTemplate('<div>{nasdaqPrice}</div><div>{nysePrice}</div>');
this.PriceTpl.compile();
}
this.PriceTpl.overwrite('market-data-div-id', marketData);
}
Not authoritative at all, but I think you're likely to be optimizing prematurely. The non-X-template method is likely somewhat less efficient, but do you have any reason to care?
Absent a description of some undesirable behavior, it sounds like you're worrying about a problem that doesn't exist now, and very likely will never exist in the future.
Keep your code clean until you have a real, present, reason to make it ugly for the sake of performance.
精彩评论