mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-13 08:59:54 +08:00
perf(jqLite): cache collection length for all methods that work on a single element
This affects jqLite#html, #text, #attr, #prop, #css and others.
This commit is contained in:
@@ -652,6 +652,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.
|
||||
@@ -661,7 +662,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);
|
||||
@@ -675,9 +676,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;
|
||||
@@ -686,7 +688,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
|
||||
|
||||
Reference in New Issue
Block a user