Hi,
When using a IOC library like ninja, is there a performance cost to this or is it a one-time hit during application_start mostly?
Hi,
When using a IOC library like ninja, is there a performance cost to this or is it a one-time hit during application_start mostly?
Are you having performance problems? Do you have specific targets to meet that you aren't meeting? Have you used a profiler to trace the performance problems to your use of an IoC framework?
If the answer to any of these questions is "no", then the real answer to your question is "it doesn't matter". If the answer to all of them is "yes", then you already know the answer.
But, yes, of course there is a performance cost to using an IoC framework. Using new
is one instruction, whereas an IoC is more than that, so it will have some cost. Does it matter to your application? Probably not. You've got the internet at one end, presumably a database at the other, and most likely some internal networking in between. Compiled code is rarely the bottleneck in web applications.
Depends on how you use it, but unless you are instantiating thousands of objects at once, there shouldn't be any noticeable bottleneck.
When an object is resolved from an IoC container, typically the container will use reflection to scan that class's constructors and public properties, and then loop through some internal collection to find the best match for each service that object requires. The instantiation is going to take as long as it would when you instantiate it manually, plus a small amount of time for the reflection calls.
If you're using a transient lifestyle, and resolving an object inside a loop, you MAY notice a slight performance hit, but at that point I'd ask if there is any better way of executing that code.
And if you haven't yet noticed a performance hit, then it shouldn't even matter to you. Don't optimize until you absolutely have to.