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

Commit

Permalink
perf(jqLite): cache collection length for all methods that work on a …
Browse files Browse the repository at this point in the history
…single element

This affects jqLite#html, #text, #attr, #prop, #css and others.
  • Loading branch information
IgorMinar authored and rodyhaddad committed Jun 13, 2014
1 parent 3cbc8e5 commit 41d2eba
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ forEach({
*/
JQLite.prototype[name] = function(arg1, arg2) {
var i, key;
var nodeCount = this.length;

// jqLiteHasClass has only two arguments, but is a getter-only fn, so we need to special-case it
// in a way that survives minification.
Expand All @@ -645,7 +646,7 @@ forEach({
if (isObject(arg1)) {

// we are a write, but the object properties are the key/values
for (i = 0; i < this.length; i++) {
for (i = 0; i < nodeCount; i++) {
if (fn === jqLiteData) {
// data() takes the whole object in jQuery
fn(this[i], arg1);
Expand All @@ -659,9 +660,10 @@ forEach({
return this;
} else {
// we are a read, so read the first child.
// TODO: do we still need this?
var value = fn.$dv;
// Only if we have $dv do we iterate over all, otherwise it is just the first element.
var jj = (value === undefined) ? Math.min(this.length, 1) : this.length;
var jj = (value === undefined) ? Math.min(nodeCount, 1) : nodeCount;
for (var j = 0; j < jj; j++) {
var nodeValue = fn(this[j], arg1, arg2);
value = value ? value + nodeValue : nodeValue;
Expand All @@ -670,7 +672,7 @@ forEach({
}
} else {
// we are a write, so apply to all children
for (i = 0; i < this.length; i++) {
for (i = 0; i < nodeCount; i++) {
fn(this[i], arg1, arg2);
}
// return self for chaining
Expand Down

0 comments on commit 41d2eba

Please sign in to comment.