Skip to content

Commit 0ff41ca

Browse files
creageivaynberg
authored andcommitted
Performance optimizations
Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
1 parent 6872664 commit 0ff41ca

1 file changed

Lines changed: 46 additions & 29 deletions

File tree

select2.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and limitations under the License.
1414
*/
15+
(function ($) {
16+
if(typeof $.fn.each2 == "undefined"){
17+
$.fn.extend({
18+
/*
19+
* 4-10 times faster .each replacement
20+
* use it carefully, as it overrides jQuery context of element on each iteration
21+
*/
22+
each2 : function (c) {
23+
var j = $([0]), i = -1, l = this.length;
24+
while (
25+
++i < l
26+
&& (j.context = j[0] = this[i])
27+
&& c.call(j[0], i, j) !== false //"this"=DOM, i=index, j=jQuery object
28+
);
29+
return this;
30+
}
31+
});
32+
}
33+
})(jQuery);
34+
1535
(function ($, undefined) {
1636
"use strict";
1737
/*global document, window, jQuery, console */
@@ -182,18 +202,19 @@
182202
}
183203

184204
function measureTextWidth(e) {
185-
var sizer, width;
205+
var sizer, width,
206+
style = e.currentStyle || window.getComputedStyle(e, null);
186207
sizer = $("<div></div>").css({
187208
position: "absolute",
188209
left: "-1000px",
189210
top: "-1000px",
190211
display: "none",
191-
fontSize: e.css("fontSize"),
192-
fontFamily: e.css("fontFamily"),
193-
fontStyle: e.css("fontStyle"),
194-
fontWeight: e.css("fontWeight"),
195-
letterSpacing: e.css("letterSpacing"),
196-
textTransform: e.css("textTransform"),
212+
fontSize: style.fontSize,
213+
fontFamily: style.fontFamily,
214+
fontStyle: style.fontStyle,
215+
fontWeight: style.fontWeight,
216+
letterSpacing: style.letterSpacing,
217+
textTransform: style.textTransform,
197218
whiteSpace: "nowrap"
198219
});
199220
sizer.text(e.val());
@@ -534,11 +555,7 @@
534555
return result.text;
535556
},
536557
formatSelection: function (data) {
537-
if (data.fullText) {
538-
return data.fullText;
539-
} else {
540-
return data.text;
541-
}
558+
return data.fullText || data.text;
542559
},
543560
formatNoMatches: function () { return "No matches found"; },
544561
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
@@ -569,14 +586,14 @@
569586
}
570587
} else if (element.is("optgroup")) {
571588
group={text:element.attr("label"), children:[]};
572-
element.children().each(function() { process($(this), group.children); });
589+
element.children().each2(function(i, elm) { process(elm, group.children); });
573590
if (group.children.length>0) {
574591
collection.push(group);
575592
}
576593
}
577594
};
578595

579-
element.children().each(function() { process($(this), data.results); });
596+
element.children().each2(function(i, elm) { process(elm, data.results); });
580597

581598
query.callback(data);
582599
});
@@ -694,11 +711,12 @@
694711

695712
ensureHighlightVisible: function () {
696713
var results = this.results, children, index, child, hb, rb, y, more;
697-
698-
children = results.find(".select2-result");
714+
699715
index = this.highlight();
700716

701717
if (index < 0) return;
718+
719+
children = results.find(".select2-result");
702720

703721
child = $(children[index]);
704722

@@ -760,9 +778,9 @@
760778
},
761779

762780
highlightUnderEvent: function (event) {
763-
var el = $(event.target).closest(".select2-result");
764-
var choices = this.results.find('.select2-result');
781+
var el = $(event.target).closest(".select2-result");
765782
if (el.length > 0) {
783+
var choices = this.results.find('.select2-result');
766784
this.highlight(choices.index(el));
767785
}
768786
},
@@ -786,7 +804,6 @@
786804
context: this.context,
787805
matcher: this.opts.matcher,
788806
callback: this.bind(function (data) {
789-
console.log("load more callback", data);
790807

791808
self.opts.populateResults(results, data.results);
792809

@@ -1113,8 +1130,8 @@
11131130

11141131
// find the selected element in the result list
11151132

1116-
this.results.find(".select2-result").each(function (i) {
1117-
if (equal(self.id($(this).data("select2-data")), self.opts.element.val())) {
1133+
this.results.find(".select2-result").each2(function (i, elm) {
1134+
if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
11181135
selected = i;
11191136
return false;
11201137
}
@@ -1172,8 +1189,8 @@
11721189
// val is an id
11731190
this.select
11741191
.val(val)
1175-
.find(":selected").each(function () {
1176-
data = {id: $(this).attr("value"), text: $(this).text()};
1192+
.find(":selected").each2(function (i, elm) {
1193+
data = {id: elm.attr("value"), text: elm.text()};
11771194
return false;
11781195
});
11791196
this.updateSelection(data);
@@ -1223,8 +1240,8 @@
12231240
// install sthe selection initializer
12241241
opts.initSelection = function (element) {
12251242
var data = [];
1226-
element.find(":selected").each(function () {
1227-
data.push({id: $(this).attr("value"), text: $(this).text()});
1243+
element.find(":selected").each2(function (i, elm) {
1244+
data.push({id: elm.attr("value"), text: elm.text()});
12281245
});
12291246
return data;
12301247
};
@@ -1492,17 +1509,17 @@
14921509
choices = this.results.find(".select2-result"),
14931510
self = this;
14941511

1495-
choices.each(function () {
1496-
var choice = $(this), id = self.id(choice.data("select2-data"));
1512+
choices.each2(function (i, choice) {
1513+
var id = self.id(choice.data("select2-data"));
14971514
if (indexOf(id, val) >= 0) {
14981515
choice.addClass("select2-disabled");
14991516
} else {
15001517
choice.removeClass("select2-disabled");
15011518
}
15021519
});
15031520

1504-
choices.each(function (i) {
1505-
if (!$(this).hasClass("select2-disabled")) {
1521+
choices.each2(function (i, choice) {
1522+
if (!choice.hasClass("select2-disabled")) {
15061523
self.highlight(i);
15071524
return false;
15081525
}

0 commit comments

Comments
 (0)