8

I can use ng-include to include a partial inside an angular view or I can use server side partials to do it on the server. I'm thinking about using server side partials instead of angular partials and then ng-include (with the script tag) because I read somewhere that angular partials create new scopes and this can hurt performance on $digest.

Is there any validity to this. What is the effect on performance when using angular includes

Harry
  • 52,711
  • 71
  • 177
  • 261

1 Answers1

9

ng-include will create a new scope and register a watch (on a path expression used by ng-include) on a scope where the ng-include is used. While this incurs some additional processing it is still JavaScript-objects based and as such is very fast. The effect of a new watch plus an additional scope should be totally negligible in most cases.

The only real difference I can see is that ng-include will include / render your partial asynchronously, so you might see a bit of delay, especially when fetching partials over the network (but this can be mitigated by pre-loading partials as described here: https://stackoverflow.com/a/12346901/1418796)

In short: in most cases the effect of ng-include should be negligible if partials are pre-loaded.

One last comment: "premature optimization is the root of all evil". Don't start micr-performance adjustments until you measure performance of your application and determine that ng-include is a bottleneck.

Community
  • 1
  • 1
pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286
  • 2
    On the other hand, ng-include(s) inside ng-repeat(s) will start very fast to show big performance degradations because of the async mecanism. The best optimization I made in my angularJS app was creating a ng-include-cached directive (synchronous when hitting cache). – Guillaume86 Feb 24 '14 at 20:43
  • @Guillaume86: Do you care to share the source code for that directive? I'd be very interested. – paldepind Feb 27 '14 at 08:00
  • 1
    @paldepind here it is: https://gist.github.com/guillaume86/9272837 but you will probably want to write a new one with the last ngInclude code from the last angular version. Mine used an old 1.0.x as the base. – Guillaume86 Feb 28 '14 at 15:17