Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select._getOptionsHtml/arrayToHTML is very slow #525

Open
smarinier opened this issue Sep 21, 2016 · 0 comments
Open

Select._getOptionsHtml/arrayToHTML is very slow #525

smarinier opened this issue Sep 21, 2016 · 0 comments

Comments

@smarinier
Copy link

smarinier commented Sep 21, 2016

When you get a huge amount of options, the time spent in these methos can reach 30s for 1500 items.
The main trouble is due to
html = html.add( $())
The array of object is destroyed/rebuit on each iteration.

Here's my fix proposal, make only one big add at the end:

Backbone.Form.editors.Select.prototype._arrayToHtml= function(array) {
    var html = [];

    //Generate HTML
    _.each(array, function(option) {
      if (_.isObject(option)) {
        if (option.group) {
          var optgroup = $j("<optgroup>")
            .attr("label",option.group)
            .html( this._getOptionsHtml(option.options) );
          html[html.length] = optgroup[0];
        } else {
          var val = (option.val || option.val === 0) ? option.val : '';
          html[html.length] = $j('<option>').val(val).text(option.label)[0];
        }
      }
      else {
        html[html.length] = $j('<option>').text(option)[0];
      }
    }, this);

    return $j().add( html);
  };
glenpike pushed a commit to glenpike/backbone-forms that referenced this issue Oct 3, 2016
As per fix, push each rendered element into an array which we
then pass to $.fn.add in one go - more efficient.
Add test to ensure that add only gets called once.
glenpike pushed a commit to glenpike/backbone-forms that referenced this issue Oct 3, 2016
As per fix, push each rendered element into an array which we
then pass to $.fn.add in one go - more efficient.
Add test to ensure that add only gets called once.
glenpike pushed a commit to glenpike/backbone-forms that referenced this issue Oct 17, 2016
For adding to array, use '[].push' instead of '[arr.length] ='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant