I know that ViewData and ViewBag both use the same backing data and that neither are as good as using strongly typed models in most cases. However when choosing between the two is the dynamic nature of ViewBag slower than using ViewData?
ViewBag vs ViewData performance difference in MVC?
26.7k Views Asked by user169867 At
2
There are 2 best solutions below
Related Questions in ASP.NET-MVC
- Can MVC.NET prevent SQL-injection at razor or controller level?
- Getting and passing MVC Model data to AngularJS controller
- Access property of an object of type [Model] in JQuery
- Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising
- Bundling and minification issue in MVC
- ASP-MVC Code-first migrations checkbox not active
- Why does Azure CloudConfigurationManager.GetSetting return null
- Dynamic roles list in CustomAuthorize ASP MVC
- Jquery: Change contents of <select> tag dynamically
- Why web API return 404 when deploy to IIS
- MVC route URL not containing parameter
- Invalidate user credentials when password changes
- MVC : Insert data to two tables
- MVC - Only allow users to edit their own data
- Submit Button on Razor View doesn't call Action Result - MVC
Related Questions in PERFORMANCE
- Slow performance on ipad erasing image
- Can Apache Ant be told to cache its XML files?
- What are the pros and cons of the picture element?
- DB candidate as CouchDB/Schema replacement
- python member str performance too slow
- Split a large query (2 days) into pieces to increase the speed in Postgres
- Use GUI displayed results of SQL query vs new queries?
- fastest way to map a large number of longs
- Bash regular expression execution hangs on long expressions
- Why is calling a function so slow in Javascript?
- Performance of element-compare in java collections
- "Capture GPU Frame" in XCode -- iOS only?
- Efficiency penalty of initializing a struct/class within a loop
- Change the rotating speed of the circle when the mouse moves using javascript
- Replace foreach to make loop into queryable
Related Questions in ASP.NET-MVC-3
- Routing Url that has no action name
- ObjectContext is disposed, can not longer used
- Image Gallery control not loading
- Skip login on MVC 3 application (SSO)
- JSON object passed to Controller is NULL
- What is the best practice for maintain a menu after a page reload? I have this custom code which feels wrong
- RedirectToAction doesn't happen when I start with an AJAX call
- Create view for a child-master relationship
- How to create edit view with a dropdownlist
- Pathname modified with IE
- Getting the property send by api in mvc
- How to create a dynamic dropdown in mvc
- set font awesome icon into textbox
- MVC3 POST model binding not working for particular complex model
- check for value null on razor syntax
Related Questions in VIEWDATA
- Unable to bind ViewData to a dropdown list
- ASP.NET Core ViewData access in javascript
- ViewData.ModelState.IsValid is false because Mvc can't parse value which has separated hundreds and thousands with commas
- Whats Difference in performance of ViewBag and View Data and when to use which one
- ASP.Net MVC-5 Editor Templates, ViewData & placeholder (watermark) results in extraneous markup
- What exactly is ViewUserControl.ViewDataKey?
- ViewBag vs ViewData performance difference in MVC?
- How do I get ViewData inside a form to display correctly?
- MVC3 ViewData Visual Basic Issue
- There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'aboutcar.Doors'
- MVC how to cast long into string in ViewData Dropdownlist
- MVC ViewData update visibility by timer
- ViewData is null in view but not in controller
- Using ViewData to pass string from Controller to View in ASP.NET MVC3
- ViewData in MVC
Related Questions in VIEWBAG
- When should I use ViewBag/model?
- Viewbag.title like "String Title"[email protected] is it possible?
- Viewbag , Printing data from hashset
- How to sort a Grid using Viewmodel not ViewBag?
- MVC c# List from string in ViewBag
- How to separate ViewBag information that is common to multiple Controllers?
- Viewdata / Viewbag is empty in editorstemplate MVC
- Error using partial view, javascript and viewbag foreach
- Whats Difference in performance of ViewBag and View Data and when to use which one
- Can I pass both the ViewBag AND a Model to a View
- Viewbag in a DisplayTemplate
- Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'
- Why does a ViewBag Object Name need to be the same name as the Drop Down it is being bound to?
- Why does an MVC view need to exist in the View directory to work?
- The name 'ViewBag' does not exist in the current context
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Okay - my initial answer basically said 'no' - time for a bit of a u-turn.
It should be 'no' in a perfect dynamic world - but upon closer inspection it would appear that there will either be no difference (accounting for JIT magic) or it might be ever-so-slightly slower, although not enough to warrant not using it (I certainly am).
In theory if properly implemented, the ViewBag would ultimately outperform the use of the ViewData dictionary because the binding of the expressions (e.g.
ViewBag.Foo) is very well cached across the different CallSites that the compiler will generate (reflect a method that does a read or write to theViewBagand you'll see what I mean).The caching layers of the DLR are well documented (if a little difficult to understand once you get in depth) but basically the runtime does its best to 'remember' where a given value instance is once its bound it - for example via a Set or Get statement.
BUT The caching, its use and effectiveness, is entirely dependent upon the underlying implementations of classes/interfaces such as DynamicObject, IDynamicMetaObjectProvider etc; as well as the end-result of the Get/Set expression binding.
In the case of the MVC internal DynamicViewDataDictionary class - it ultimately ends up binding to this:
For
var a = ViewBag.FooAnd
For
ViewBag.Foo = Bar;In other words - the statements are effectively being rewritten to wrappers around the dictionary indexer.
Because of that, there's certainly no way it could be faster than doing it yourself.
Were
ViewDatato feed off ofViewBag, instead of the other way around, and hadViewBagthen been implemented with even something likeExpandoObject, then it might be a different story - as the dynamic implementation ofExpandoObjectis much more intelligent and the caching rules it employs allow for some pretty cool runtime optimisations.In Conclusion
(thanks to Shawn McLean for suggesting one was needed!)
ViewBag will be slower than ViewData; but probably not enough to warrant concern.