Skip to content

Commit 12e0de2

Browse files
committed
changes on updateResults, populate by @cervengoc on select2#781
use string concatenation instead of DOM manipulation at populate. This does gives about a 45% speed boost as measured with chrome v35. Code by @cervengoc on select2#781
1 parent 52425f9 commit 12e0de2

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

select2.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -919,39 +919,46 @@ the specific language governing permissions and limitations under the Apache Lic
919919

920920
populate=function(results, container, depth) {
921921

922-
var i, l, result, selectable, disabled, compound, node, label, innerContainer, formatted;
922+
var i, l, result, selectable, disabled, compound, node, label, innerContainer, formatted, formattedClass;
923923

924924
results = opts.sortResults(results, container, query);
925925

926+
// collect the created nodes for bulk append
927+
var nodes = [];
926928
for (i = 0, l = results.length; i < l; i = i + 1) {
927929

928930
result=results[i];
929-
930931
disabled = (result.disabled === true);
931932
selectable = (!disabled) && (id(result) !== undefined);
932-
933933
compound=result.children && result.children.length > 0;
934934

935-
node=$("<li></li>");
936-
node.addClass("select2-results-dept-"+depth);
937-
node.addClass("select2-result");
938-
node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
939-
if (disabled) { node.addClass("select2-disabled"); }
940-
if (compound) { node.addClass("select2-result-with-children"); }
941-
node.addClass(self.opts.formatResultCssClass(result));
942-
node.attr("role", "presentation");
943-
944-
label=$(document.createElement("div"));
945-
label.addClass("select2-result-label");
946-
label.attr("id", "select2-result-label-" + nextUid());
947-
label.attr("role", "option");
948-
949-
formatted=opts.formatResult(result, label, query, self.opts.escapeMarkup);
950-
if (formatted!==undefined) {
951-
label.html(formatted);
952-
node.append(label);
953-
}
954-
935+
node ="<li class=\"";
936+
node += "select2-results-dept-" + depth;
937+
node += " select2-result";
938+
node+= selectable ? " select2-result-selectable" : " select2-result-unselectable";
939+
if (disabled)
940+
node += " select2-disabled";
941+
if (compound)
942+
node += " select2-result-with-children";
943+
formattedClass = self.opts.formatResultCssClass(result);
944+
if (formattedClass != undefined)
945+
node += " " + formattedClass;
946+
node += "\" role=\"presentation\">";
947+
948+
label = "<div class=\"select2-result-label\"";
949+
label += " id=\"select2-result-label-" + nextUid() + "\"";
950+
label += " role=\"option\"";
951+
label += ">";
952+
formatted = opts.formatResult(result, label, query, self.opts.escapeMarkup);
953+
if (formatted !== undefined)
954+
label += formatted;
955+
label += "</div>";
956+
957+
node += label;
958+
node += "</li>";
959+
960+
// I still used jQuery wrapping for setting "data" below
961+
node = $(node);
955962

956963
if (compound) {
957964

@@ -962,9 +969,10 @@ the specific language governing permissions and limitations under the Apache Lic
962969
}
963970

964971
node.data("select2-data", result);
965-
container.append(node);
972+
nodes.push(node[0]);
966973
}
967-
974+
// bulk append the created nodes
975+
container.append(nodes);
968976
liveRegion.text(opts.formatMatches(results.length));
969977
};
970978

0 commit comments

Comments
 (0)