Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know the performance difference between Razor, Spark, and NVelocity view engines?

Has anyone done any have any performance numbers that compare the various viewengine implementations for ASP.NET MVC3? Specifically I'm interested in the differences in performance between Razor, Spark, and NVelocity. I realize the last is a bit dated, but we use it for generating emails, and would like to replace it with RazorEngine.

NVelocity used to be fast in its day, but I'm suspicious of its performance now as compared to the other, more modern viewengines.

Any insight would be appreciated.

like image 402
steve_c Avatar asked Oct 10 '22 09:10

steve_c


2 Answers

One of the authors of RazorEngine here. The first compilation of a view is always slow. After that it's cached and should be fairly fast. There's a new version coming out eventually that will be a basic rewrite. In addition I've already written a much smaller/simpler/hopefully faster version with the help of Sam Saffron. (from this site) That said, I haven't done any performance tests of RazorEngine vs any other engine.

Razor itself is compiled (after the first call). If Spark and others are also compiled they might be just as fast. But at first glance I would assume* Razor to be faster after the first call due to native code vs. parsing the file on each call.

The simplest way to test would be to profile each one and make that determination for yourself. Everybody's environment will be different which could yield different results.

*We all know what assuming does

like image 103
Buildstarted Avatar answered Oct 13 '22 11:10

Buildstarted


I know it's an old question, but after looking for better alternatives to razor I still think none of them is faster than nvelocity. Razor gives you intellisense and is easier to code. But nvelocity is MUCH faster. In the example below you'll find that razor takes 2 seconds to parse one line of text. NVelocity parses instantly. I've tested this with template files as well, same result. 2 seconds to parse is too slow for a commercial web application. So despite the fact that I like razor a lot more, I have no choice than to stick with NVelocity.

I'm not very sure about caching. But in the case of string parsing (as opposed to file based parsing) as in the example below, there is no caching for razor.

Simple asp.net example:

protected void Page_Load(object sender, EventArgs e)
{
    string toParseText = "bladiebla $Model.SomeProperty";
    dynamic model = new ExpandoObject();
    model.SomeProperty = "hello";
    string result = Razor.Parse(toParseText, model);


    var _templateEngine = new VelocityEngine();
    _templateEngine.Init();
    var context = new VelocityContext();
    context.Put("Model", model);
    using (StringWriter writer = new StringWriter())
    {
        _templateEngine.Evaluate(context, writer, "", toParseText);
        string result2 = writer.ToString();
    }
}

NuGet:

  • install-package RazorEngine
  • install-package nvelocity
like image 43
user1154148 Avatar answered Oct 13 '22 10:10

user1154148