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 answers
Related Questions in ASP.NET-MVC
- I have a problem outputing the roles on the page ListRoles.cshtml
- Dropdown list showing SQLServer2005SQLBrowserUser$DONSERVER instead of Active Directory group name in ASP.NET MVC C#
- Hosting ASP.NET MVC application on IIS web server using Windows 2019 server
- How to display only department fields associated with a selected department in student automation system?
- How to send select input data for form submission?
- Multi level project reference using dll
- How to upload file to Onedrive using ASP.NET MVC?
- ASP.NET MVC web app looping between fields only on some devices
- Is there any automatic job to load AD-groups?
- How to restrict admin js files to download
- Download PDF in ASP.NET MVC application
- How to add bootstrap theme/example into ASP.NET MVC 5?
- Web API works with Windows authentication enabled when consumed via Swagger but throws an unauthorized issue when accessed through web app
- ASP.Net Core 7.0 Web App (Model-View-Controller) ErrorViewModel OnGet OnPost do not get called or executed
- OAuth 2.0 keep getting Authorization has been denied for this request
Related Questions in PERFORMANCE
- Upsert huge amount of data by EFCore.BulkExtensions
- How can I resolve this error and work smoothly in deep learning?
- Efficiently processing many small elements of a collection concurrently in Java
- Theme Preloader for speed optimization in WordPress
- I need help to understand the time wich my simple ''hello world'' is taking to execute
- Non-blocking state update
- Do conditional checks cause bottlenecks in Javascript?
- Performance of sketch drastically decreases outside of the P5 Web Editor
- sample query for review for improvement on big query
- Is there an indexing strategy in Postgres which will operate effectively for JOINs with ORs
- Performance difference between two JavaScript code snippets for comparing arrays of strings
- C++ : Is there an objective universal way to compare the speed of iterative algorithms?
- How to configure api http request with load testing
- the difference in terms of performance two types of update in opensearch
- Sveltekit : really long to send the first page and intense CPU computation
Related Questions in ASP.NET-MVC-3
- Insert new user AspNetUser from another controller in .NET 7
- Error HRESULT E_FAIL has been returned from a call to a COM component in Asp.net MVC
- ASP.NET MVC 3/4 Razor - public action method not found - how to debug
- get data by linq but have Error when doit this
- ASP.NET issue facing exception issue
- How to Implement a View Count Feature for a Blog Website in Asp.net MVC Using a Database?
- Unable to install ASP.NET MVC 3 using chocolatey
- Display data from DB to ajaxToolkit LINE CHART (Ajax control Toolkit v20.1) Using ASP.NET Webform C# (Dissertation Question)
- Why ModelState Isvalid false when working on images
- How to create an ASP.NET MVC 3.2.7 page with 3 view models
- How to make correct test project
- MVC Increase max number of concurrent simultaneous request per user
- Error Number:2705,State:4,Class:16 Code first approach
- Integrating the SSO for MVC3 application with AzureADB2C account
- How to integrate Asp.Net MVC existing application to MAUI Framework
Related Questions in VIEWDATA
- How to Post New Record to Table and Pass that ID to another page to Create a Related Record
- Handling multiple items of data on the same view page
- How do pass value of a list using ViewBag in a partial view?
- I am unable to see the Full Data(Analysis -> View data) in Tableau
- asp.net webapp - is viewdata safe for local request variable?
- How do I render js code in .cshtml file based on C# value
- I want to read an array from the controller and use it in an Html script
- Show different View Data in view with different class in ASP.NET Core
- Putting data from two tables into one ViewData
- Want to display a value of a loop outside of the loop as a heading in Razor pages
- Why do we have ViewBag and ViewData, if they are doing the same thing in ASP.NET Core MVC
- How to pass a ViewBag/ViewData from a PartialView to the parent view in ASP.NET Core?
- The Name 'ViewData' does not exist in the current context in a razor page
- SelectBox does not show me the data transferred from ViewData dictionary
- Return State and city on Drop down list in ASP .NET Core
Related Questions in VIEWBAG
- Populating textarea using ViewBag ASP.NET Core MVC
- Display the displayName view
- Why is my ViewBag.CustomerName coming up null?
- Loading Viewbag data list from Json Result
- Viewbag from controller (JsonResult) to razor view as hidden field
- CS0103 The name 'ViewBag' does not exist in the current context mvc 5
- does not show error message on view data in ASP.NET
- TempData doesn't carry the data in second action
- How to avoid using viewbag in ASP.NET Core
- How do pass value of a list using ViewBag in a partial view?
- How can I pass the value of a list into a partial view using ViewBag?
- C# MVC Dropdownlist using IEnumerable Visualizer
- Change onclick method in mvc submit button
- ViewBag values from Database displaying as whiteout
- ASP.NET Core MVC : @Html.DropDownList on change pass ViewBag as parameter
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?
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)
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.