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

Commit 6d418ef

Browse files
IgorMinarrodyhaddad
authored andcommitted
perf(jqLite): cache collection length for all methods that work on a single element
This affects jqLite#html, #text, #attr, #prop, #css and others.
1 parent 3fa1606 commit 6d418ef

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/jqLite.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ forEach({
652652
*/
653653
JQLite.prototype[name] = function(arg1, arg2) {
654654
var i, key;
655+
var nodeCount = this.length;
655656

656657
// jqLiteHasClass has only two arguments, but is a getter-only fn, so we need to special-case it
657658
// in a way that survives minification.
@@ -661,7 +662,7 @@ forEach({
661662
if (isObject(arg1)) {
662663

663664
// we are a write, but the object properties are the key/values
664-
for (i = 0; i < this.length; i++) {
665+
for (i = 0; i < nodeCount; i++) {
665666
if (fn === jqLiteData) {
666667
// data() takes the whole object in jQuery
667668
fn(this[i], arg1);
@@ -675,9 +676,10 @@ forEach({
675676
return this;
676677
} else {
677678
// we are a read, so read the first child.
679+
// TODO: do we still need this?
678680
var value = fn.$dv;
679681
// Only if we have $dv do we iterate over all, otherwise it is just the first element.
680-
var jj = (value === undefined) ? Math.min(this.length, 1) : this.length;
682+
var jj = (value === undefined) ? Math.min(nodeCount, 1) : nodeCount;
681683
for (var j = 0; j < jj; j++) {
682684
var nodeValue = fn(this[j], arg1, arg2);
683685
value = value ? value + nodeValue : nodeValue;
@@ -686,7 +688,7 @@ forEach({
686688
}
687689
} else {
688690
// we are a write, so apply to all children
689-
for (i = 0; i < this.length; i++) {
691+
for (i = 0; i < nodeCount; i++) {
690692
fn(this[i], arg1, arg2);
691693
}
692694
// return self for chaining

0 commit comments

Comments
 (0)