Reputation: 2538
I use dependency injection in Angular 2 often to provide data service and in-memory caching of data objects. It is natural that I have one injectable for each type of purpose. As my project grows, this question occurs to me:
If I put all injectable functions and parameters in one big class, and inject it to all my components, how will the performance be affected?
This could illustrate what I mean:
say if I have injectable A
, B
and C
and components a
, b
and c
(could be many more, but you get the idea). Now A
is injected to a
; B
is injected to b
and C
is injected to c
. If I combine all injectables to ABC
and inject it to components a
, b
, c
, how would this affect my performance?
Upvotes: 2
Views: 492
Reputation: 42669
Keeping performance aside, there is another problem with an umbrella object containing all the dependencies.
It can make code less readable as it becomes a little difficult to analyze and determine what are the external dependencies for a component. We need to look through the code and references to understand a component's dependency. Not an ideal implementation i believe.
Upvotes: 1
Reputation: 657721
If I put all injectable functions and parameters in one big class, and inject it to all my components, how will the performance be affected?
If you use AoT, then this will have no measurable effect, because AoT generates static code for DI.
If you use platform_dynamic (without AoT) then there might be a small difference.
For your approach to make any sense, you would need to do benchmark tests with and without the change to get serious feedback about whether this has any effect on performance.
In general prefer code quality over performance unless you can demonstrate that a particular code hurts performance (don't do premature optimizations).
If you can change your code so that you need a single server request less than before, you'll probably save more time than all DI needs together.
Upvotes: 6