fix(injector): small perf improvement & code cleanup

This commit is contained in:
Igor Minar 2011-12-16 12:45:20 -08:00 committed by Misko Hevery
parent 97dae0d0a0
commit 1e96d0af8c
2 changed files with 15 additions and 11 deletions

View file

@ -384,25 +384,29 @@ function createInjector(modulesToLoad) {
function invoke(fn, self, locals){
var args = [],
$injectAnnotation,
$injectAnnotationIndex,
$inject,
length,
key;
if (typeof fn == 'function') {
$injectAnnotation = inferInjectionArgs(fn);
$injectAnnotationIndex = $injectAnnotation.length;
$inject = inferInjectionArgs(fn);
length = $inject.length;
} else {
if (isArray(fn)) {
$injectAnnotation = fn;
$injectAnnotationIndex = $injectAnnotation.length;
fn = $injectAnnotation[--$injectAnnotationIndex];
$inject = fn;
length = $inject.length - 1;
fn = $inject[length];
}
assertArgFn(fn, 'fn');
}
while($injectAnnotationIndex--) {
key = $injectAnnotation[$injectAnnotationIndex];
args.unshift(locals && locals.hasOwnProperty(key) ? locals[key] : getService(key));
for(var i = 0; i < length; i++) {
key = $inject[i];
args.push(
locals && locals.hasOwnProperty(key)
? locals[key]
: getService(key, path)
);
}
// Performance optimization: http://jsperf.com/apply-vs-call-vs-invoke

View file

@ -52,7 +52,7 @@ describe('injector', function() {
injector.get('s1');
expect(log).toEqual(['s6', 's5', 's3', 's4', 's2', 's1']);
expect(log).toEqual(['s6', 's3', 's5', 's4', 's2', 's1']);
});