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

View file

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