This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Performance optimations #389
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
cdd6a83
Avoid .call operations where unnecessary
kpdecker 6f8f81c
Avoid bind when possible, favoring closures
kpdecker dd18147
Optimize destroy logic
kpdecker 926659a
Optimize template context generation
kpdecker fd41ff9
Optimize the url helper
kpdecker 4231542
Optimize event registration
kpdecker 029494b
Use _resetIdCounter for uniqueId
kpdecker 13a2bbb
Strip the getOptionsData helper
kpdecker 7d792fc
Use Object.create for context cloning
kpdecker 3c5aa0a
Avoid intermediate object in tag util
kpdecker 223de8f
Avoid .apply calls where possible
kpdecker b74f196
Avoid depot cases in registerViewHelper helper
kpdecker d386459
Defer element append handler unless necessary
kpdecker 2c016ec
Remove data object bind/unbind return value
kpdecker cf8f488
Optimize renderCollection append item usage
kpdecker 6c4b2c8
Optimize collection helper event forwarding
kpdecker be4f6f9
Avoid class deopt in View constructor
kpdecker 1bad6c8
Avoid try deoptimization in render
kpdecker 791fc97
Move forms into separate mixin
kpdecker b20dcc6
Remove unnecessary vars
kpdecker 8b19381
Use _ rather than collection forEach
kpdecker 28210f7
Avoid duplicate variable
kpdecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,9 @@ var _fetch = Backbone.Collection.prototype.fetch, | |
|
||
Thorax.Collection = Backbone.Collection.extend({ | ||
model: Thorax.Model || Backbone.Model, | ||
initialize: function() { | ||
initialize: function(models, options) { | ||
this.cid = _.uniqueId('collection'); | ||
return Backbone.Collection.prototype.initialize.apply(this, arguments); | ||
return Backbone.Collection.prototype.initialize.call(this, models, options); | ||
}, | ||
isEmpty: function() { | ||
if (this.length > 0) { | ||
|
@@ -37,7 +37,7 @@ Thorax.Collection = Backbone.Collection.extend({ | |
collection._fetched = true; | ||
success && success(collection, response, options); | ||
}; | ||
return _fetch.apply(this, arguments); | ||
return _fetch.call(this, options); | ||
}, | ||
set: function(models, options) { | ||
this._fetched = !!models; | ||
|
@@ -102,10 +102,10 @@ Thorax.CollectionView = Thorax.View.extend({ | |
} | ||
}, | ||
|
||
render: function() { | ||
render: function(output) { | ||
var shouldRender = this.shouldRender(); | ||
|
||
Thorax.View.prototype.render.apply(this, arguments); | ||
Thorax.View.prototype.render.call(this, output); | ||
if (!shouldRender) { | ||
this.renderCollection(); | ||
} | ||
|
@@ -192,7 +192,7 @@ Thorax.CollectionView = Thorax.View.extend({ | |
//appendItem(model [,index]) | ||
//appendItem(html_string, index) | ||
//appendItem(view, index) | ||
appendItem: function(model, index, options) { | ||
appendItem: function(model, index, options, append) { | ||
//empty item | ||
if (!model) { | ||
return; | ||
|
@@ -213,7 +213,8 @@ Thorax.CollectionView = Thorax.View.extend({ | |
itemView = model; | ||
model = false; | ||
} else { | ||
index = index || collection.indexOf(model) || 0; | ||
index = index != null ? index : (collection.indexOf(model) || 0); | ||
|
||
// Using call here to avoid v8 prototype inline optimization bug that helper views | ||
// expose under Android 4.3 (at minimum) | ||
// https://twitter.com/kpdecker/status/422149634929082370 | ||
|
@@ -229,7 +230,7 @@ Thorax.CollectionView = Thorax.View.extend({ | |
//if the renderer's output wasn't contained in a tag, wrap it in a div | ||
//plain text, or a mixture of top level text nodes and element nodes | ||
//will get wrapped | ||
if (_.isString(itemView) && !itemView.match(/^\s*</m)) { | ||
if (_.isString(itemView) && !/^\s*</m.test(itemView)) { | ||
itemView = '<div>' + itemView + '</div>'; | ||
} | ||
var itemElement = itemView.$el || $($.trim(itemView)).filter(function() { | ||
|
@@ -243,13 +244,18 @@ Thorax.CollectionView = Thorax.View.extend({ | |
'data-model-cid': model.cid | ||
}); | ||
} | ||
var previousModel = index > 0 ? collection.at(index - 1) : false; | ||
if (!previousModel) { | ||
$el.prepend(itemElement); | ||
|
||
if (append) { | ||
$el.append(itemElement); | ||
} else { | ||
//use last() as appendItem can accept multiple nodes from a template | ||
var last = $el.children('[' + modelCidAttributeName + '="' + previousModel.cid + '"]').last(); | ||
last.after(itemElement); | ||
var previousModel = index > 0 ? collection.at(index - 1) : false; | ||
if (!previousModel) { | ||
$el.prepend(itemElement); | ||
} else { | ||
//use last() as appendItem can accept multiple nodes from a template | ||
var last = $el.children('[' + modelCidAttributeName + '="' + previousModel.cid + '"]').last(); | ||
last.after(itemElement); | ||
} | ||
} | ||
|
||
this.trigger('append', null, function($el) { | ||
|
@@ -263,7 +269,7 @@ Thorax.CollectionView = Thorax.View.extend({ | |
this.trigger('rendered:item', this, collection, model, itemElement, index); | ||
} | ||
if (filter) { | ||
applyItemVisiblityFilter.call(this, model); | ||
applyItemVisiblityFilter(this, model); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rare case when performance optimization also makes the code more clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So little faith! |
||
} | ||
} | ||
return itemView; | ||
|
@@ -317,32 +323,34 @@ Thorax.CollectionView = Thorax.View.extend({ | |
renderCollection: function() { | ||
if (this.collection) { | ||
if (this.collection.isEmpty()) { | ||
handleChangeFromNotEmptyToEmpty.call(this); | ||
handleChangeFromNotEmptyToEmpty(this); | ||
} else if (this._pendingRestore) { | ||
// If we had to delay the initial restore due to the local data set being loaded, then | ||
// we want to resume that operation where it left off. | ||
this._pendingRestore = false; | ||
this.restoreCollection(this._forceRerender); | ||
} else { | ||
handleChangeFromEmptyToNotEmpty.call(this); | ||
this.collection.forEach(function(item, i) { | ||
this.appendItem(item, i); | ||
}, this); | ||
handleChangeFromEmptyToNotEmpty(this); | ||
|
||
var self = this; | ||
_.each(this.collection.models, function(item, i) { | ||
self.appendItem(item, i, undefined, true); | ||
}); | ||
} | ||
this.trigger('rendered:collection', this, this.collection); | ||
} else { | ||
handleChangeFromNotEmptyToEmpty.call(this); | ||
handleChangeFromNotEmptyToEmpty(this); | ||
} | ||
}, | ||
emptyClass: 'empty', | ||
renderEmpty: function() { | ||
if (!this.emptyView) { | ||
assignView.call(this, 'emptyView', { | ||
assignView(this, 'emptyView', { | ||
extension: '-empty' | ||
}); | ||
} | ||
if (!this.emptyTemplate && !this.emptyView) { | ||
assignTemplate.call(this, 'emptyTemplate', { | ||
assignTemplate(this, 'emptyTemplate', { | ||
extension: '-empty', | ||
required: false | ||
}); | ||
|
@@ -370,13 +378,13 @@ Thorax.CollectionView = Thorax.View.extend({ | |
|
||
renderItem: function(model, i) { | ||
if (!this.itemView) { | ||
assignView.call(this, 'itemView', { | ||
assignView(this, 'itemView', { | ||
extension: '-item', | ||
required: false | ||
}); | ||
} | ||
if (!this.itemTemplate && !this.itemView) { | ||
assignTemplate.call(this, 'itemTemplate', { | ||
assignTemplate(this, 'itemTemplate', { | ||
extension: '-item', | ||
// only require an itemTemplate if an itemView | ||
// is not present | ||
|
@@ -445,28 +453,37 @@ Thorax.CollectionView = Thorax.View.extend({ | |
}, | ||
|
||
updateFilter: function() { | ||
applyVisibilityFilter.call(this); | ||
var view = this; | ||
if (view.itemFilter) { | ||
_.each(view.collection.models, function(model) { | ||
applyItemVisiblityFilter(view, model); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
Thorax.CollectionView.on({ | ||
restore: 'restoreCollection', | ||
|
||
collection: { | ||
reset: onCollectionReset, | ||
sort: onCollectionReset, | ||
'reset': function(collection) { | ||
onCollectionReset(this, collection); | ||
}, | ||
'sort': function(collection) { | ||
onCollectionReset(this, collection); | ||
}, | ||
change: function(model) { | ||
var options = this.getObjectOptions(this.collection); | ||
if (options && options.change) { | ||
this.updateItem(model); | ||
} | ||
applyItemVisiblityFilter.call(this, model); | ||
applyItemVisiblityFilter(this, model); | ||
}, | ||
add: function(model) { | ||
var $el = this._collectionElement; | ||
if ($el.length) { | ||
if (this.collection.length === 1) { | ||
handleChangeFromEmptyToNotEmpty.call(this); | ||
handleChangeFromEmptyToNotEmpty(this); | ||
} | ||
|
||
var index = this.collection.indexOf(model); | ||
|
@@ -476,7 +493,10 @@ Thorax.CollectionView.on({ | |
remove: function(model) { | ||
var $el = this._collectionElement; | ||
this.removeItem(model); | ||
this.collection.length === 0 && $el.length && handleChangeFromNotEmptyToEmpty.call(this); | ||
|
||
if (this.collection.length === 0 && $el.length) { | ||
handleChangeFromNotEmptyToEmpty(this); | ||
} | ||
} | ||
} | ||
}); | ||
|
@@ -494,56 +514,50 @@ Thorax.View.on({ | |
} | ||
}); | ||
|
||
function onCollectionReset(collection) { | ||
function onCollectionReset(view, collection) { | ||
// Undefined to force conditional render | ||
var options = this.getObjectOptions(collection) || undefined; | ||
if (this.shouldRender(options && options.render)) { | ||
this.renderCollection && this.renderCollection(); | ||
var options = view.getObjectOptions(collection) || undefined; | ||
if (view.shouldRender(options && options.render)) { | ||
view.renderCollection && view.renderCollection(); | ||
} | ||
} | ||
|
||
// Even if the view is not a CollectionView | ||
// ensureRendered() to provide similar behavior | ||
// to a model | ||
function onSetCollection(collection) { | ||
function onSetCollection(view, collection) { | ||
// Undefined to force conditional render | ||
var options = this.getObjectOptions(collection) || undefined; | ||
if (this.shouldRender(options && options.render)) { | ||
var options = view.getObjectOptions(collection) || undefined; | ||
if (view.shouldRender(options && options.render)) { | ||
// Ensure that something is there if we are going to render the collection. | ||
this.ensureRendered(); | ||
} | ||
} | ||
|
||
function applyVisibilityFilter() { | ||
if (this.itemFilter) { | ||
this.collection.forEach(applyItemVisiblityFilter, this); | ||
view.ensureRendered(); | ||
} | ||
} | ||
|
||
function applyItemVisiblityFilter(model) { | ||
var $el = this._collectionElement; | ||
this.itemFilter && $el.find('[' + modelCidAttributeName + '="' + model.cid + '"]')[itemShouldBeVisible.call(this, model) ? 'show' : 'hide'](); | ||
function applyItemVisiblityFilter(view, model) { | ||
var $el = view._collectionElement; | ||
view.itemFilter && $el.find('[' + modelCidAttributeName + '="' + model.cid + '"]')[itemShouldBeVisible(view, model) ? 'show' : 'hide'](); | ||
} | ||
|
||
function itemShouldBeVisible(model) { | ||
function itemShouldBeVisible(view, model) { | ||
// Using call here to avoid v8 prototype inline optimization bug that helper views | ||
// expose under Android 4.3 (at minimum) | ||
// https://twitter.com/kpdecker/status/422149634929082370 | ||
return this.itemFilter.call(this, model, this.collection.indexOf(model)); | ||
return view.itemFilter.call(view, model, view.collection.indexOf(model)); | ||
} | ||
|
||
function handleChangeFromEmptyToNotEmpty() { | ||
var $el = this._collectionElement; | ||
this.emptyClass && $el.removeClass(this.emptyClass); | ||
function handleChangeFromEmptyToNotEmpty(view) { | ||
var $el = view._collectionElement; | ||
view.emptyClass && $el.removeClass(view.emptyClass); | ||
$el.removeAttr(collectionEmptyAttributeName); | ||
$el.empty(); | ||
} | ||
|
||
function handleChangeFromNotEmptyToEmpty() { | ||
var $el = this._collectionElement; | ||
this.emptyClass && $el.addClass(this.emptyClass); | ||
function handleChangeFromNotEmptyToEmpty(view) { | ||
var $el = view._collectionElement; | ||
view.emptyClass && $el.addClass(view.emptyClass); | ||
$el.attr(collectionEmptyAttributeName, true); | ||
this.appendEmpty(); | ||
view.appendEmpty(); | ||
} | ||
|
||
//$(selector).collection() helper | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
micro opt: Avoids indexOf lookup for
index === 0
case. Likely fast but felt like a why not avoid the call sort of case.