title: Slow DisplayFor performance in ASP.NET Core MVC
body: <p>In my current application I'm generating a fairly lengthy table to display to the user. I've been seeing some serious performance issues with it, which I've tracked down to usage of @Html.DisplayFor, and I'm not entirely sure why.</p>
  
  <p>Edit: I've replaced the code sample with a more concise and reproducible setup.</p>
  
  <p>In order to isolate the issue, I have created a new asp.net core MVC project using all default settings in visual studio, with no authentication. I created a view model as such:</p>
  
  <pre><code>public class TestingViewModel
  {
      public int Id { get; set; }
      public string TextValue1 { get; set; }
      public string TextValue2 { get; set; }
  }
  </code></pre>
  
  <p>Then added a controller which fills the view model with data to pass to the view: </p>
  
  <pre><code>    public IActionResult TestThings()
      {
          var list = new List&lt;TestingViewModel&gt;();
          for(var i = 0; i &lt; 1000; i++)
              list.Add(new TestingViewModel {Id = i, TextValue1 = "Test", TextValue2 = "Test2"});
  
          return View(list);
      }
  </code></pre>
  
  <p>The view is the bare minimum possible to display the data:</p>
  
  <pre><code>@model List&lt;DisplayForTest.ViewModels.TestingViewModel&gt;
  
  @foreach (var item in Model)
  {
      @Html.DisplayFor(m =&gt; item.Id)
      @Html.DisplayFor(m =&gt; item.TextValue1)
      @Html.DisplayFor(m =&gt; item.TextValue2)
  }
  </code></pre>
  
  <p>When running this code, it takes over one second to run! The culprit is the DisplayFor. If I change the view as follows: </p>
  
  <pre><code>@model List&lt;DisplayForTest.ViewModels.TestingViewModel&gt;
  
  @foreach (var item in Model)
  {
      @item.Id
      @item.TextValue1
      @item.TextValue2
  }
  </code></pre>
  
  <p>This renders in 13ms. It's clear DisplayFor is adding a huge amount of time to rendering... on my PC that's nearly 0.4ms per call. While that's not bad in isolation, it makes it a pretty bad choice for lists or other things.</p>
  
  <p>Is <code>DisplayFor</code> really just that slow? Or am I using it incorrectly?</p>