Non
Non

Reputation: 8589

Turn $scope functions into regular var functions. Has any impact in the performance?

I have around 6 functions in my code that are not interacting with anything in the DOM, and I need those only in one specific scope.

So, is there any issue if I turn this

$scope.verifyPlaceBetAvailable = function(param) {
  //something happens here
}

into this

var verifyPlaceBetAvailable = function(param) {
  //something happens here
}

?

I mean, that is going to have any impact in the performance of my app?

Upvotes: 0

Views: 95

Answers (5)

Darshan Patel
Darshan Patel

Reputation: 2899

You don't have to put every method in controller in scope.

As long as the method is not used in view or not interacting with anything in the DOM than you can remove that function from scope. There is no major performance issue.

For utility methods you should use factory,service or provider in angular js.

Refer AngularJS: Service vs provider vs factory

Upvotes: 2

Eugene Gluhotorenko
Eugene Gluhotorenko

Reputation: 3164

You should add functions in $scope only if you need them in a template otherwise use local variables or injected services if it has some business logic and does not require $scope. And it is rather improves app performance than decreases it.

Upvotes: 2

Daniel Cottone
Daniel Cottone

Reputation: 4480

You only need to put a function in the scope if you are calling it from a view. Any other type of function could be a simple var in the controller, however many of these functions should probably be in services or factories, and not directly implemented in the controller anyway.

Upvotes: 1

Nestor Britez
Nestor Britez

Reputation: 1218

First of all, you should put all helpers or methods into a service, such as factory, service or provider.

About performance, I think the issue is not what you're asking. I believe can impact more about how they're written and what kind of data they're processing.

For 6 simple methods it shouldn't affect to the performance.

Upvotes: 2

icchio
icchio

Reputation: 109

I think if you don't populate the $scope with function that will not have affect on it or on the DOM should be better....

Upvotes: 2

Related Questions