mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-09 22:39:39 +08:00
perf(jqLite): optimize adding nodes to a jqLite collection
This code is very hot and in most cases we are wrapping just a single Node so we should optimize for that scenario.
This commit is contained in:
@@ -370,17 +370,31 @@ function jqLiteAddClass(element, cssClasses) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function jqLiteAddNodes(root, elements) {
|
||||
// THIS CODE IS VERY HOT. Don't make changes without benchmarking.
|
||||
|
||||
if (elements) {
|
||||
elements = (!elements.nodeName && isDefined(elements.length) && !isWindow(elements))
|
||||
? elements
|
||||
: [ elements ];
|
||||
for(var i=0; i < elements.length; i++) {
|
||||
root.push(elements[i]);
|
||||
|
||||
// if a Node (the most common case)
|
||||
if (elements.nodeType) {
|
||||
root[root.length++] = elements;
|
||||
} else {
|
||||
var length = elements.length;
|
||||
|
||||
// if an Array or NodeList and not a Window
|
||||
if (typeof length === 'number' && elements.window !== elements) {
|
||||
if (length) {
|
||||
push.apply(root, elements);
|
||||
}
|
||||
} else {
|
||||
root[root.length++] = elements;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function jqLiteController(element, name) {
|
||||
return jqLiteInheritedData(element, '$' + (name || 'ngController' ) + 'Controller');
|
||||
}
|
||||
|
||||
@@ -963,6 +963,8 @@ describe('jqLite', function() {
|
||||
},
|
||||
detachEvent: noop
|
||||
};
|
||||
window.window = window;
|
||||
|
||||
var log;
|
||||
var jWindow = jqLite(window).on('hashchange', function() {
|
||||
log = 'works!';
|
||||
|
||||
Reference in New Issue
Block a user