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

Commit 41d2eba

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 3cbc8e5 commit 41d2eba

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
@@ -636,6 +636,7 @@ forEach({
636636
*/
637637
JQLite.prototype[name] = function(arg1, arg2) {
638638
var i, key;
639+
var nodeCount = this.length;
639640

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

647648
// we are a write, but the object properties are the key/values
648-
for (i = 0; i < this.length; i++) {
649+
for (i = 0; i < nodeCount; i++) {
649650
if (fn === jqLiteData) {
650651
// data() takes the whole object in jQuery
651652
fn(this[i], arg1);
@@ -659,9 +660,10 @@ forEach({
659660
return this;
660661
} else {
661662
// we are a read, so read the first child.
663+
// TODO: do we still need this?
662664
var value = fn.$dv;
663665
// Only if we have $dv do we iterate over all, otherwise it is just the first element.
664-
var jj = (value === undefined) ? Math.min(this.length, 1) : this.length;
666+
var jj = (value === undefined) ? Math.min(nodeCount, 1) : nodeCount;
665667
for (var j = 0; j < jj; j++) {
666668
var nodeValue = fn(this[j], arg1, arg2);
667669
value = value ? value + nodeValue : nodeValue;
@@ -670,7 +672,7 @@ forEach({
670672
}
671673
} else {
672674
// we are a write, so apply to all children
673-
for (i = 0; i < this.length; i++) {
675+
for (i = 0; i < nodeCount; i++) {
674676
fn(this[i], arg1, arg2);
675677
}
676678
// return self for chaining

0 commit comments

Comments
 (0)