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.

Christian Soto
  • 2,540
  • 2
  • 15
  • 33

2 Answers2

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.

Bk Santiago
  • 1,523
  • 3
  • 13
  • 24
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.

Shohel
  • 3,886
  • 4
  • 38
  • 75