aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShyam Seshadri2010-06-02 15:05:34 -0700
committerShyam Seshadri2010-06-02 15:05:34 -0700
commit39312d1fe3a27b248f98f6f26577fcd7e2c64f85 (patch)
tree244c0f72c1685da2db10f2bb2a2da49816829306
parenta29c5e4c7fd5e708c28e70e974bf873621d5277c (diff)
downloadangular.js-39312d1fe3a27b248f98f6f26577fcd7e2c64f85.tar.bz2
Revert "Revert "removed few key foreach and replaced thime with for loop for performance.""
This reverts commit a29c5e4c7fd5e708c28e70e974bf873621d5277c.
-rw-r--r--scenario/perf.html3
-rw-r--r--src/Scope.js21
-rw-r--r--src/directives.js51
3 files changed, 44 insertions, 31 deletions
diff --git a/scenario/perf.html b/scenario/perf.html
index e4edc00f..cd676918 100644
--- a/scenario/perf.html
+++ b/scenario/perf.html
@@ -24,7 +24,8 @@
<input type="text" name="text"/>
<hr/>
<ul>
- <li ng-repeat="item in items.$filter('').$orderBy('name')">
+ <li Xng-repeat="item in items.$filter('').$orderBy('name')"
+ ng-repeat="item in items">
{{item.name}} <a href="#{{item.name}}">{{item.parts.join(', ')}}</a>
</li>
</ul>
diff --git a/src/Scope.js b/src/Scope.js
index 1c223130..637fc25e 100644
--- a/src/Scope.js
+++ b/src/Scope.js
@@ -53,14 +53,15 @@ function getterFn(path){
var code = 'function (self){\n';
code += ' var last, fn, type;\n';
foreach(path.split('.'), function(key) {
+ key = (key == 'this') ? '["this"]' : '.' + key;
code += ' if(!self) return self;\n';
code += ' last = self;\n';
- code += ' self = self.' + key + ';\n';
+ code += ' self = self' + key + ';\n';
code += ' if(typeof self == "function") \n';
- code += ' self = function(){ return last.'+key+'.apply(last, arguments); };\n';
- if (key.charAt(0) == '$') {
+ code += ' self = function(){ return last'+key+'.apply(last, arguments); };\n';
+ if (key.charAt(1) == '$') {
// special code for super-imposed functions
- var name = key.substr(1);
+ var name = key.substr(2);
code += ' if(!self) {\n';
code += ' type = angular.Global.typeOf(last);\n';
code += ' fn = (angular[type.charAt(0).toUpperCase() + type.substring(1)]||{})["' + name + '"];\n';
@@ -123,11 +124,13 @@ function createScope(parent, services, existing) {
if (exp !== undefined) {
return expressionCompile(exp).apply(instance, slice.call(arguments, 1, arguments.length));
} else {
- foreach(evalLists.sorted, function(list) {
- foreach(list, function(eval) {
- instance.$tryEval(eval.fn, eval.handler);
- });
- });
+ for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) {
+ for ( var queue = evalLists.sorted[i],
+ jSize = queue.length,
+ j= 0; j < jSize; j++) {
+ instance.$tryEval(queue[j].fn, queue[j].handler);
+ }
+ }
}
},
diff --git a/src/directives.js b/src/directives.js
index 91a98735..cabf0c23 100644
--- a/src/directives.js
+++ b/src/directives.js
@@ -24,14 +24,17 @@ angularDirective("ng-eval", function(expression){
angularDirective("ng-bind", function(expression){
return function(element) {
- var lastValue, lastError;
+ var lastValue = noop, lastError = noop;
this.$onEval(function() {
var error,
value = this.$tryEval(expression, function(e){
error = toJson(e);
}),
- isHtml = value instanceof HTML,
- isDomElement = isElement(value);
+ isHtml,
+ isDomElement;
+ if (lastValue === value && lastError == error) return;
+ isHtml = value instanceof HTML,
+ isDomElement = isElement(value);
if (!isHtml && !isDomElement && isObject(value)) {
value = toJson(value);
}
@@ -72,14 +75,14 @@ function compileBindTemplate(template){
});
bindTemplateCache[template] = fn = function(element){
var parts = [], self = this;
- foreach(bindings, function(fn){
- var value = fn.call(self, element);
+ for ( var i = 0; i < bindings.length; i++) {
+ var value = bindings[i].call(self, element);
if (isElement(value))
value = '';
else if (isObject(value))
value = toJson(value, true);
parts.push(value);
- });
+ };
return parts.join('');
};
}
@@ -107,21 +110,26 @@ var REMOVE_ATTRIBUTES = {
};
angularDirective("ng-bind-attr", function(expression){
return function(element){
+ var lastValue = {};
this.$onEval(function(){
- foreach(this.$eval(expression), function(bindExp, key) {
- var value = compileBindTemplate(bindExp).call(this, element),
+ var values = this.$eval(expression);
+ for(var key in values) {
+ var value = compileBindTemplate(values[key]).call(this, element),
specialName = REMOVE_ATTRIBUTES[lowercase(key)];
- if (specialName) {
- if (element[specialName] = toBoolean(value)) {
- element.attr(specialName, value);
+ if (lastValue[key] !== value) {
+ lastValue[key] = value;
+ if (specialName) {
+ if (element[specialName] = toBoolean(value)) {
+ element.attr(specialName, value);
+ } else {
+ element.removeAttr(key);
+ }
+ (element.data('$validate')||noop)();
} else {
- element.removeAttr(key);
+ element.attr(key, value);
}
- (element.data('$validate')||noop)();
- } else {
- element.attr(key, value);
}
- }, this);
+ };
}, element);
};
});
@@ -153,17 +161,18 @@ angularWidget("@ng-repeat", function(expression, element){
var children = [], currentScope = this;
this.$onEval(function(){
- var index = 0, childCount = children.length, childScope, lastElement = reference;
- foreach(this.$tryEval(rhs, reference), function(value, key){
+ var index = 0, childCount = children.length, childScope, lastElement = reference,
+ collection = this.$tryEval(rhs, reference);
+ for ( var key in collection) {
if (index < childCount) {
// reuse existing child
childScope = children[index];
- childScope[valueIdent] = value;
+ childScope[valueIdent] = collection[key];
if (keyIdent) childScope[keyIdent] = key;
} else {
// grow children
childScope = template(element.clone(), createScope(currentScope));
- childScope[valueIdent] = value;
+ childScope[valueIdent] = collection[key];
if (keyIdent) childScope[keyIdent] = key;
lastElement.after(childScope.$element);
childScope.$index = index;
@@ -174,7 +183,7 @@ angularWidget("@ng-repeat", function(expression, element){
childScope.$eval();
lastElement = childScope.$element;
index ++;
- });
+ };
// shrink children
while(children.length > index) {
children.pop().$element.remove();