More performative way to return data to View

3

In a view where we return a list of objects, and we need to demonstrate the quantity, such as a small report, using some conditions.

What is the most performative way to return data to view ?

Explaining better.

When we return an object list to view , for example:

Using the Microsoft example , we have:

public class Movie 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public DateTime ReleaseDate { get; set; } 
    public string Genre { get; set; } 
    public decimal Price { get; set; } 
} 

And we return all Movies registered in our controller , for example:

public ActionResult Index() 
        { 
            return View(db.Movies.ToList()); 
        } 

In this case, let's say we want to know the amount of movies and show in a table, we could bring in a ViewBag like this:

  public ActionResult Index() 
        { 
            var filmes = db.Movies.ToList();
            ViewBag.TotoalFilmes = filmes.Count();
            return View(filmes); 
        }  

And in view only show ViewBag , like this:

<p> Total: @ViewBag.TotalFilmes</p>

This is one way to do it, but another would be simply using Count() directly in view , like this:

<p> Total: @Model.Count</p>

Here's my question: Which of the two forms is "better" (more performative)?

Remembering that in the example I just showed with a ViewBag . But I wonder if we have a lot of data.

If you have a third way, which is better, I'll be happy to get to know you too.

    
asked by anonymous 24.07.2015 / 19:40

1 answer

1
Essentially it makes no difference. It may, and I just said that it may, there is a slight difference between one or the other, but it will be irrelevant, especially near the whole that will be executed.

It will be difficult to measure reliably and only measurements can tell what is actually faster. I think it will not make any difference. Certainly nothing that is noticeable.

If you are experiencing performance issues, it will not be this way; if you are not having problems, it is a micro-optimization > useless. Do what makes the most sense for the code, which is more readable for you, which is better manutibility.

In some situation, where the code may be relevant, but not in this or most of them. Even this relevance is not so strong. And if it matters, you probably have a much bigger problem in the code.

I have my doubts if it would work exactly as you put it, but that is not relevant to the question.

    
24.07.2015 / 19:54