views:

74

answers:

2

How much is the actual memory / performance gain in an ASP.NET MVC controller when using these two different ways of declaring the Model for a view?

User user = userService.GetByID(id);
return View(user);

or

return View(userService.GetById(id));

I'm supposing that the last one is a bit more performant as we do not initialize an object, however the first one is more readable. Would this even matter on a webserver with thousands of visitors?

+2  A: 

There is zero difference between the two statements as far as what memory is allocated, and the behavior of the garbage collector. Use the one you prefer to read :-)

Joel Martinez
+2  A: 

Actually, you initialize an object in both cases; what the first is doing that the second is not is: 1) reserve some space for a variable (which doesn't concern performance, but space); 2) add a reference to the object (that is, increment the number of references for that object) and then remove the reference (decrement the number of references) the row after.

I would hardly believe that a performance difference is observable between the two.

That said, I prefer the second, as there's no need for the user variable, there; anyway, it's just a matter of taste, there may be styling reasons to prefer the first and other ones to prefer the second.

giorgian
well, I don't know if `userService.GetById` creates an object or returns an existing one, but I hope I made myself clear...
giorgian