0

I had a discussion with a colleague about when to use directives. I say that it is better to use directives only when you need DOM manipulation or a controller, in the other cases you should use plain HTML (or at best ng-include) in order to avoid adding more useless complexity to the code

He says you should use a directive almost everywhere, that is, even for static elements with no logic, because it increases readability in the code.

We started looking for info against or in favor of our points (such as AngularJS: ngInclude vs directive ) but we couldn't find anything about performance about directives. So my question is: how much and in which ways does a directive affect performance?

Community
  • 1
  • 1
valepu
  • 3,136
  • 7
  • 36
  • 67
  • The question is incorrect. ngInclude *is* the directive. – Estus Flask Nov 26 '15 at 12:51
  • ngInclude is _a_ directive. But all it does is include a template VS a directive that adds a link, a controller and potentially a new scope. I'll try to make it clear in the question – valepu Nov 26 '15 at 12:53
  • ngInclude adds [link](https://github.com/angular/angular.js/blob/v1.4.8/src/ng/directive/ngInclude.js#L199), [controller](https://github.com/angular/angular.js/blob/v1.4.8/src/ng/directive/ngInclude.js#L193) and [creates a new scope](https://github.com/angular/angular.js/blob/v1.4.8/src/ng/directive/ngInclude.js#L236). You should decide on custom directive's implementation that you want to compare to ngInclude before asking the question. It is 'apples to ???' comparison. – Estus Flask Nov 26 '15 at 13:12
  • then please make an answer saying these things (though the controller is an empty function in this case) adding details about the performance and how a directive impacts it and answering the 3rd part (plain HTML) – valepu Nov 26 '15 at 13:16
  • the problem that @estus is trying to point to is that the question is flawed. Since `ng-include` is itself a directive, it cannot possibly perform differently than a custom directive, unless the code in said custom directive adds complexity. Therefore, knowing what code the directive contains is required to evaluate performance conditions. – Claies Nov 26 '15 at 14:37
  • Yes, i am well aware of what @estus is trying to say. Though i was hoping that ng-include used some trick or was in some way optimized since it comes directly from angular but i have realized, by reading around, that this is not the case. Anyways my question, though badly worded, was never meant to be a ng-include VS rest of the world, rather i want to know in which ways a directive (any directive) might affect performance. I have edited my question again in order to be clearer – valepu Nov 26 '15 at 14:41
  • I don't think that the question can get a quality answer in its current state. There are too many factors that may affect directive performance. ngInclude is quite complex for a simple thing it does, the best thing you can do is to learn how it is works and why it has 120 lines of code and not 20. It never was rewritten from scratch. It tries to be as compatible with other built-in directives and services. And it accepts a variable template name. The one can surely get less overhead in directive with 'templateUrl' only if that's what is required in current case. – Estus Flask Nov 26 '15 at 15:21

0 Answers0