0

I had one question during interviews.

"Which of the following code snippets is more efficient?"

AppModule.controller('homeController', ['$scope', 'dep1', 'dep2', function($scope, dep1, dep2) {
...
    $scope.aMethod = function() {
       ...
    }
...
}]);

AppModule.controller('homeController', function($scope, dep1, dep2) {
...
    $scope.aMethod = function() {
        ...
    }
...
});

• Both are equally efficient.

• The second code is more efficient as it contains less code.

• The first code is more efficient because it holds names of dependencies.

• The second code is more efficient because it not contains additional an array.

I think the 3rd answer is right but not sure I'm right.

Please help me.

3

2 Answers 2

2

The first one will work as expected after applying some uglification/minification while the second one won't work after that since the variable names will change during this process and angularjs will have no way to know what to inject since the variable names changed.

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

Comments

1

Third is right, Because when you will uglification/minification the code by using any task runner then angular have no way to know the dependency. so third is better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.