The docs recommend to register frequently-used components via lambdas as ...
This can yield an improvement of up to 10x faster
Resolve()
calls
Now there are obviously a few questions:
Being anonymous, lambda functions can be easily passed without being assigned to a variable. Lambda functions are inline functions and thus execute comparatively faster.
If a function is CPU-, network- or memory-bound, then changing the memory setting can dramatically improve its performance. Since the Lambda service charges for the total amount of gigabyte-seconds consumed by a function, increasing the memory has an impact on overall cost if the total duration stays constant.
Summary. Just by chance of inspecting how layers impact Lambda execution time it came out that the quickest execution for cold Lambda was still about 32 times slower that the quickest for hot Lambda (e.g. with layers: ~978ms/29ms).
As noted in the comments, the short version is that the concrete implementation is going to be faster than the reflection manner of resolving.
Diving deeper, think about the steps involved in each.
Lambda:
Reflection:
As you can see, there's actually a lot more work than just Activator.CreateInstance
in the reflection manner of resolving, which is why it takes longer.
But, as also noted in the comments, don't worry about premature optimization. This all happens pretty darn quickly so wait to optimize until you can actually locate a bottleneck using a profiler or some similar tool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With