Paul
Paul

Reputation: 2530

MVC FormCollection Performance Speed

I am working on a page that has a dynamically generated markup. And as such, I have no idea what fields are available on the form. So instead of creating a view model, the POST method uses the FormCollection. Once the POST is received, the program runs quickly.

My problem: Clicking SUBMIT on the form will take about 10 - 15 seconds to hit a breakpoint on the first line of the POST action in the controller. In my scenario, there are 800 input fields on the form. Does it really take this long to create the FormCollection for only 800 fields? Are there any ways to streamline the process or improve the response time?

EDIT:

The IE profiler indicates the majority of time is spent in the JQuery CSS selector. If I remove all CSS, the page works very quickly. Why is the CSS selector running prior to posting back to the server? Can I disable it?

EDIT 2: The profiler is now showing time spend in JQuery 1.6.4.min.js, the function is 'k'. I think that is just an internal varialble lookup. I have also found that removing the 'size' and 'maxlength' attributes from the input fields makes the page respond quickly (hooray)... except... I need those for client side validation. I'm still trying to find a way to improve the speed, or remove the attributes and retain some way to limit the input length of the fields on the client side.

Edit 3: I've dug into the jquery-1.6.4.min.js and found function k. Per Edit 2, adding/removing the 'maxlength' and 'size' attributes makes all the difference (33K ms instead of 3k ms). However, function K should not do much... as those are not nodetype===1 and the first IF should fail, returning out of the function on the next line.

function k(a,c,d)
    {
        if(d===b&&a.nodeType===1)
        {
            var e="data-"+c.replace(j,"-$1").toLowerCase();
            d=a.getAttribute(e);
            if(typeof d=="string")
            {
                try
                {
                    d=d==="true"?!0:d==="false"?!1:d==="null"?null:f.isNaN(d)?i.test(d)?f.parseJSON(d):d:parseFloat(d)
                }
                catch(g){}
                f.data(a,c,d)
            }
            else 
                d=b
        }
        return d
    }

Upvotes: 0

Views: 568

Answers (2)

Fred Christianson
Fred Christianson

Reputation: 151

I just created a form with 1000 fields and the controller can access each field in less than a second from when I submit.

This is how I create the fields:

<script language="javascript" type="text/javascript">
$(document).ready(function () {
    for (var i = 0; i < 1000; i++) {
    $('<input/>').attr({ type: 'text', id: 'test'+i, name: 'test'+i,value:'test '+i }).appendTo('#fields');
    }
    });
</script>

and this is the action:

    public ActionResult TestFields()
    {
        for (int i = 0; i < 1000; i++)
        {
            if (Request.Form[i] != string.Format("test{0}", i))
            {
                throw new Exception("bad value received");
            }
        }
            return View();
    }

Populating the FormCollection with form values doesn't seem to be the problem. I have short values and testing on local network. If you have longer values and a slow network, that could be the problem. If each of your values is 1KB, you need to send 800KB.

Upvotes: 1

Paul
Paul

Reputation: 2530

From comments with Mystere Man, it turns out somehow the client side was validating, this link solved the issue: http://forums.asp.net/t/1523883.aspx/1/10?Disabling+client+side+validation+on+submit+button+

Basically, I disabled the validation on the submit button. This allows the 'maxlength' and 'size' attributes to work when the user types, but prevents any client side validation on the POST. This behavior seems fine, as the server will validate again as the input is processed. The average time for the POST changed from 33 seconds to 2 seconds.

Also here: Disable client-side validation in MVC 3 "cancel" submit button

Upvotes: 0

Related Questions