MVC Performance and AJAX

Although MVC is unsuitable for small websites, it is generally very good for complicated, application-like sites. However, there are some circumstances in which using an MVC framework is great for most of the application, but the performance overheads spoil certain aspects.

AJAX requests are a good example of this, especially when a template engine is enforced and all served pages have to be created using it.

An example

An example is 'find as you type' functionality in which search results are being populated as the keys are pressed. Here, requests are being sent to the server on each and every key-press (or after an instant of no continuous key-presses, being efficient) but in either case many requests are being sent to the server in a very short period of time. It ruins the point of such functionality if every key press takes seconds to update.

The simplest solution is often the best...

The request handler for the search functionality can be very lightweight. In this circumstance it may even be worth using just a single script containing the SQL and server-side code to get and format the results. The fewer files that are included and the less data that are passed from array to array the better, to ensure the fastest possible response.

Network traffic aside, we're talking about a serious performance overhead when strictly using an MVC framework for all parts of an application.

How much slower?

To give you an idea of the difference in performance, we're talking about an overhead that can result in a request taking 10 to 30 times longer - seconds vs a few dozen milliseconds. It doesn't sound like a lot but it can really ruin the effect.

Now, I'm not saying that all MVC frameworks will be this slow, and indeed many have AJAX-specific functionality that does help with performance problems. What I am saying that that it's not always a good idea to rely 100% on a MVC framework for all aspects of an application.

Why the overheads?

Many MVC frameworks are implemented in the same server-side, interpreted language that you would normally code in. It runs on top of the web server (and its modules) and your application mostly runs on top of all that.

Conclusion

In an ideal world the MVC framework and template engine would run as a compiled web or application server module, then performance overheads wouldn't really be an issue. Until then we need to make use of what we have and, most importantly, use the right tool for the job.