Hello,

I've put together a fast form serializer function that I'm hoping can
get some review from the list for completeness, bugs, a better name,
etc. A previous thread revealed quite a performance issue with the form
plugin's existing serialize function when operating on a form with a
large number of elements. My plugin sacrifices semantic order for wicked
speed. I have a test form with a select menu containing 500 options --
with the current serializer, it takes around 5 full seconds to do the
job, and with mine, it takes about 50 ms. Any feedback would be much
appreciated, including whether you think this should be included with
the form plugin distribution.

Since this isn't a plugin, I'm just going to include the source in this
message -- please let me know if there's a better (i.e., more polite)
way to do this.

Thanks!

m.

---

$.fn.fastSerialize = function() {
    var a = [];
    var f = ['INPUT', 'TEXTAREA', 'SELECT', 'BUTTON'];

    $(f.join(','), this).each(function() {
        var n = this.name || this.id;
        var t = this.type;

        if (!n || this.disabled || t == 'reset' ||
           (t == 'checkbox' || t == 'radio') && !this.checked ||
           (t == 'submit' || t == 'image' || t == 'button') &&
this.form.clicked != this ||
           this.tagName == 'SELECT' && this.selectedIndex == -1)
            return;

        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );

        if (t == 'select-multiple') {
            $('option', this).each( function() {
                if (this.selected)
                    a.push({name: n, value: this.value ||
$(this).text()});
            });
            return;
        }

        a.push({name: n, value: this.value});
    });

    return a;
};

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to