Dustin
Dustin

Reputation: 8267

Does polluting the $scope object affect performance?

I have a controller where the $scope object has been used to store methods and values that are only used locally within the same controller. There is a lot of this going on:

$scope.foo = 'something';
$scope.bar = 'something else';

... and so on. None of these values are used within the view. My question is does polluting the $scope object affect performance? Is it a good idea to clean this up so only values and methods needed for the view are contained in the $scope object?

Upvotes: 4

Views: 545

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

Yes, Polluting $scope does affect performance, but its depends your scope has multiple watchers which are frequently changing then that will create a more overhead cost. Refer this answer which has covered same point

For avoiding this situation I'd suggest you to do good re-factoring of code

Handle all the logic in controller whenever required otherwise do separate a logic by making good use of each component.

  1. Move common method(logic) to service/factory/provider which is used in multiple place, so that it would be sharable.
  2. If some value are fixed, they are not gonna change then move them to constant/value
  3. Whenever you feel like you have same logic which needs to be keep in $scope it self then move that logic to common controller. When required you could inject in your current controller scope using $controller injector

Also refer Understanding Of Scope for clear understanding of use of scope

Upvotes: 4

Related Questions