ASP.NET MVC partial views slow?

前端 未结 3 1705
走了就别回头了
走了就别回头了 2021-02-05 07:21

I just happen to check the performance of an ASP.NET MVC application we are building. I was going to insert a partial view into a loop, and just out of curiosity I checked how l

相关标签:
3条回答
  • 2021-02-05 07:30

    I am guessing the answer is ... it depends?

    Partial views decrease performance (the overhead of the actual call etc).

    Partial views are not cached.

    Including a partial view inside a loop will decrease performance, and can be sped up again by moving the loop inside the partialview instead.

    Some sample reading (which references the caching of the viewpath) can be found here.

    0 讨论(0)
  • 2021-02-05 07:45

    60ms is such a small interval it sounds like statistical noise to me, not conclusive evidence of a performance difference.

    0 讨论(0)
  • 2021-02-05 07:53

    I've just changed a MVC2 view from using a partial view in a loop to a single view, i.e. :

    <table>
    foreach(var a in items)
    {
      <%: Html.Partial("SomePartialView",a) %>
    }
    </table>
    

    Where SomePartialView contains the code to render a single row in a table, e.g. :

    <tr><td>Model.Name</td><td>Model.description</td></tr>
    

    to :

    foreach(var a in items)
    {
      <tr><td>a.Name</td><td>a.description</td></tr>
    }
    

    for a view rendering 900 rows the page render time went down from 5+ minutes page load to less than 30 secs, pretty conclusive proof that there is a significant overhead when calling partial views. I'm sure this is negligible when you have a single call, however in a loop it all adds up so I'd recommended avoiding partial views in a loop if possible.

    0 讨论(0)
提交回复
热议问题