Here ng-show/ng-hide would be so much better, because
1) it does not open a new scope (ng-if does)
2) it does not remove/add elements from the DOM (ng-if does)
I believe that the only reason where ng-if is valid in that template would be when you combine it with compile or any other heavy computation.
Second: Use Angular One Time Binding
I also think that you should make heavy use of Angulars one time binding syntax, using :: where it makes sense. One time binding will avoid creating a watcher.
This will create a watcher for colDefinitions, and in addition to col.cellTemplate for each column.
In addition, this is within an ng-repeat of all rows, so you can multiply the amount of watchers...
As a counter example where you would NOT want one time binding to be active:
Showing the sorting icon depending on whether the column is sorted or not:
Third: Use ng-repeat with track by
ng-repeat makes heavy computation using a hash function. By using track-by with a unique identifier you can avoid this computation. for instance, ng-repeat on the column definitions could be tracked by their index, using
ng-repeat=\"col in ::colDefinitions track by $index\"
I'm waiting a little before I send a Pull Request, in case other people find reasons on why this or that should not be handled the way I proposed it. PLEASE DISCUSS!
For simplicity so people can try it out, here is my modified template:
Hi!
I would like to propose several performance tweaks to this directive!
First (ng-show vs ng-if): I would suggest avoiding ng-if for showing/hiding icons. For instance here: https://github.com/khan4019/tree-grid-directive/blob/master/src/tree-grid-directive.js#L13
<a ng-if=\"col.sortable\" ng-click=\"sortBy(col)\">{{col.displayName || col.field}}</a><span ng-if=\"!col.sortable\">{{col.displayName || col.field}}</span>
Here ng-show/ng-hide would be so much better, because 1) it does not open a new scope (ng-if does) 2) it does not remove/add elements from the DOM (ng-if does)
I believe that the only reason where ng-if is valid in that template would be when you combine it with
compile
or any other heavy computation.Second: Use Angular One Time Binding I also think that you should make heavy use of Angulars one time binding syntax, using :: where it makes sense. One time binding will avoid creating a watcher.
For instance, look at this code:
This will create a watcher for colDefinitions, and in addition to col.cellTemplate for each column. In addition, this is within an ng-repeat of all rows, so you can multiply the amount of watchers...
As a counter example where you would NOT want one time binding to be active: Showing the sorting icon depending on whether the column is sorted or not:
Third: Use ng-repeat with track by ng-repeat makes heavy computation using a hash function. By using track-by with a unique identifier you can avoid this computation. for instance, ng-repeat on the column definitions could be tracked by their index, using
ng-repeat=\"col in ::colDefinitions track by $index\"
I'm waiting a little before I send a Pull Request, in case other people find reasons on why this or that should not be handled the way I proposed it. PLEASE DISCUSS!
For simplicity so people can try it out, here is my modified template: