6

Difference between this vs $scope is clearly answered in this question. One of the projects I am working in, Our senior guy is promoting this over $scope arguing it provides better performance.

I tried to find any evidence for the same but official angular documents do have very limited information on this matter.

Can any one please explain

  1. Is this provide better performance over $scope (provided I don't use $watch etc)
  2. If yes for what exact reasons ? is the performance improvement significant enough to change an existing application using $scope to this ?
Community
  • 1
  • 1
Yogesh
  • 4,546
  • 2
  • 32
  • 41
  • possible duplicate of [this vs $scope in AngularJS controllers](http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers) – Roy M J Jul 15 '15 at 05:56
  • please read question clearly, Am not asking for deference I am asking some thing else – Yogesh Jul 15 '15 at 05:57
  • 4
    refer this: https://github.com/johnpapa/angular-styleguide – Tarun Dugar Jul 15 '15 at 05:58
  • Stylistically it is better to use `this` than `$scope` as that way when you do use a `$watch` or similar it is a giant heads up as that controller takes in `$scope` for a reason, otherwise you tend to ignore it is there. Also, people tend to use things like `$scope.$apply` and similar if it's available - they'll find "better" ways if it's not. Performance wise, probably no benefit at all. – Kyle Muir Jul 15 '15 at 06:08
  • I am not sure about performance, but definetily $scope is a syntactic sugar over native this. Now a days Angular team is promoting controller as which uses this instead of $scope. The real benefit is when you migrate from angular 1.* to 2.0. Other benefit includes decoupling controller from the framework, so that you can test the controller out of angular context. – Abhishek Prakash Jul 15 '15 at 06:39

3 Answers3

5

AngularJS performance is affected by the number of binding the currently loaded view (page) is using and the watches that you setup manually using $watch. All this binding only work on properties declared on $scope.

This means if you are not binding a property to view or not watching it, you better not declare it on the $scope (also called avoiding scope pollution).

Coming to this, as explained in the SO post this has different context when invoked by Angular (such as in case of ng-click) and when controller is created.

So anything that you declare on this (when referring to controller) technically cannot be bound to the view as it is not declared on the scope.

But Angular came up with a controller as syntax where it allow us to use properties and method over the controller object. In such scenario properties declared over controller are bound in the view using ctrl.prop syntax. Internally Angular does something like this when you do ng-controller='HomeController as ctrl'

$scope.ctrl=this

Which basically means Angular is attaching the complete controller object to the $scope and hence binding with controller properties work.

So only thing that matter in terms of performance is the number of binding being watched.

Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • so in short there is no performance benefit of `this` over `$scope`!? – Yogesh Jul 29 '15 at 10:16
  • As mentioned performance is defined by the number of binding in view, not directly by `this` or `$scope`. If you define a number of properties on scope and never use it, then it does not affect the performance. – Chandermani Jul 29 '15 at 10:19
  • 2
    In the case, isint it better to use `$scope` instead of `this`? If I understand correctly, if `controller as` syntax exposes the entire `this` to `$scope`, doesnt that cause more `$scope` pollution that the method where we only declare `$scope` in controller when we need it? – Neel Nov 21 '15 at 13:57
0

Angularjs uses both of them interchangeably so there's no difference regarding the performance.

But using this should be lower performance (not even so lower) than the $scope as this creates a new instance of the object.

But really using this is more elegant way as:

  • More contextual (for eg. items.name than name)
  • Can be used nested controller easily without any conflicts.
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

Assuming you don't have watches it shouldn't create a performance barrier.

In my opinion, you should not have any "this" methods\properties on your controller.
A controller is the view's best friend and should only have relevant properties on it.
A relevant property is a property that you are going to bind inside your view (properties, functions etc.).

All your "this" properties should probably be on a service.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99