Prohibitively slow render performance with large select elements
I've found that the render performance of this library tends to be prohibitively slow for select elements with large numbers of options. From my tests, the "setSelected" and "setDisabled" functions contribute to over 90% of the render time.
Current tests show that rendering takes 40ms per select tag on average, but often takes 250ms+ to render when using larger select elements (196 options for a country dialog).
Same here. Unusable for a country select, especially when you repeat it several times on a page. The browser's watchdog quickly proposes to kill the script.
Found this while experiencing the same issue. On a developer spec machine in Firefox, selecting an item in a large list takes an estimated 2 seconds. Searching the list is actually significantly quicker than selecting an item.
The issue is that the enter control is re-rendered on every select. And the complexity of render() is O(n). So the time grows linearly. On my system, the mix and max per iteration time for one of the loops in render() is 1ms to 58ms. That's a pretty large variance.
Re-rendering on every select is overkill. Unfortunately I've spent too much time on this already, and won't be able to refactor the code.
Just by saving the list of LIs instead of doing a find on each setSelected and setDisabled cut down the time for render() by 60%
setSelected: function setSelected(index, selected) {
if (this.$lis == null) this.$lis = this.$menu.find('li');
$(this.$lis[index]).toggleClass('selected', selected);
},
setDisabled: function setDisabled(index, disabled) {
if (this.$lis == null) this.$lis = this.$menu.find('li');
if (disabled) {
$(this.$lis[index]).addClass('disabled').find('a').attr('href','#').attr('tabindex',-1);
} else {
$(this.$lis[index]).removeClass('disabled').find('a').removeAttr('href').attr('tabindex',0);
}
},
@pembo13 Thanks for this, I also implemented this solution and it works great. I would just comment that on both the "update" and the "refresh" functions you will probably want to set the list to null so it will recompute the values as it should.
Just a question about this issue, why this awesome code from @pembo13 hasn't been integrated into the master, I tried it and it worked very well on my test, saved a lot of loading time !! Still not enough but good enough for the first 2 selects used on a single page.
I am loading more than 10 selects of 1600 items on one page, We should think about something lighter :)
+1
@cifren It was implemented...back in January of 2014.
Sorry, maybe I am wrong but I tried to find this in the master branch source and nothing than I could see was there. I had to change the code with the one given above and it changed completely the behaviour, not enough but faster. Anyway, I am sorry I went to the enemy... I used Select2... But thanks for your work and the sharing of this tool :)
@cifren https://github.com/silviomoreto/bootstrap-select/blob/master/js/bootstrap-select.js#L746-L758
Please try v1.7.0-rc2 and report back.
@caseyjhol : I have tried with the recent version(v1.7.2-0) but still facing the same issue. I was not able to display 341 cities in select box ,it takes long time.
Can you provide me with a JSFiddle or link to an example? What browser/OS are you using? I tested using nearly 1000 cities on Windows 8/Chrome/Firefox and it loads almost instantaneously.
@caseyjhol : http://jsfiddle.net/nr54vx7b/88/ , I am using Windows 7 / Chrome Version 43.0.2357.134m
That's because you're trying to call refresh() every time you add an option. You want to add all of the options first, and then call refresh once outside of the for loop.
In addition, you're not using the newest version of bootstrap-select that has all of the performance enhancements.
http://jsfiddle.net/nr54vx7b/90/
Oh Ok Thanks for the help @caseyjhol . Code which you suggested works good .
https://github.com/silviomoreto/bootstrap-select/blob/master/js/bootstrap-select.js#L746-L758
this plugin is not working for updation or edit???
it doesnt select multiple options which we already selected while editing???