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:
Igor Minar
2014-06-05 05:52:09 -07:00
committed by rodyhaddad
parent e35abc9d2f
commit 31faeaa729
2 changed files with 21 additions and 5 deletions

View File

@@ -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');
}

View File

@@ -963,6 +963,8 @@ describe('jqLite', function() {
},
detachEvent: noop
};
window.window = window;
var log;
var jWindow = jqLite(window).on('hashchange', function() {
log = 'works!';