Chuntao Fu
Chuntao Fu

Reputation: 35

Which is more efficient: traditional routing or attribute routing?

I am recently doing projects using ASP.NET MVC 5. It is very easy and convenient to using attribute routing. I am wondering which is more efficient compare attribute routing with traditional routing approach in a large application.

Upvotes: 2

Views: 454

Answers (2)

theMayer
theMayer

Reputation: 16177

I would say, as the answer to so many questions, "it depends."

Efficiency is defined as the amount of useful output you receive from a process over the total amount of effort expended. So, by that definition, I would focus on human efficiency, since one hour of programmer time probably equates roughly to a month's worth of computing power on Amazon EC2.

According to Microsoft:

When the route definitions are co-located with the actions, within the same source file rather than being declared on an external configuration class, it can make it easier to reason about the mapping between URIs and actions.

I would go a step beyond this and say it is way easier to figure out what goes where, and what does what. It's saved me countless hours to do it this way; in fact, I was so frustrated with the centralized mapping strategy that I converted all of my services to use the attribute-based format.

Upvotes: 0

Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8464

The MVC routing is cached at startup only once. There is a tiny bit of performance penalty to perform reflection on startup. After startup, there would be no difference. When I say tiny, anything from 0.1ms to 1 or 2 seconds for a large website.

But don't pre-optimise at this point. MVC is all based on reflection anyway.

The benefits of keeping your routes next to your controller's actions (for maintenance) far exceeds a tiny performance overhead it introduces.

Upvotes: 3

Related Questions