2

Which of the following two cases do you think is better in terms of performance (having many dependencies to inject in a service)?

app.service('randomService', function(dependency1, dependency2, ...) {
    //code
});

or

app.service('randomService', function($injector) {
    var service;
    this.svFct = function (entityType, idList, entityList) {
            switch (entityType) {
                    case 'product':
                        service = $injector.get('dependency1');
                        service.get({}, onSuccess, onError);
                        break;
                    case 'order':
                        service = $injector.get('dependency2');
                        service.get({}, onSuccess, onError);
                        break;
                    case 'actor':
                        service = $injector.get('dependency3');
                        service.get({}, onSuccess, onError);
                        break;
                     ...
                    }
    };
});
1
  • This is not a duplicate. The question marked as dupe is about testing and angular.mock.inject, it has nothing to do with the subject. Commented Apr 18, 2017 at 12:40

1 Answer 1

4

Performance impact of Angular injector is negligible. Services are singletons which are instantiated on first injection and retrieved from key/value storage on subsequent injections.

The comparison is incorrect since two snippets don't contain the same logic. svFct method can be called multiple times and will do $injector.get(...) on each call.

In first case there is one injector call. In second case there are multiple injector calls.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.