Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
perf(jqLite): optimize adding nodes to a jqLite collection
Browse files Browse the repository at this point in the history
This code is very hot and in most cases we are wrapping just a single Node so
we should optimize for that scenario.
  • Loading branch information
IgorMinar authored and rodyhaddad committed Jun 13, 2014
1 parent e35abc9 commit 31faeaa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 2 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,8 @@ describe('jqLite', function() {
},
detachEvent: noop
};
window.window = window;

var log;
var jWindow = jqLite(window).on('hashchange', function() {
log = 'works!';
Expand Down

0 comments on commit 31faeaa

Please sign in to comment.