From 720bdb8c4f0c75f17410b6f182343948d89aef61 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 12 Sep 2012 14:47:23 -0400 Subject: [PATCH 1/3] Store last mouse position in a variable instead of document datastore It's hundred times faster. See http://jsperf.com/data-vs-variable --- select2.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/select2.js b/select2.js index 521629dc..35385cad 100644 --- a/select2.js +++ b/select2.js @@ -40,7 +40,7 @@ return; } - var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer; + var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer, lastMousePosition; KEY = { TAB: 9, @@ -163,7 +163,7 @@ } $(document).delegate("body", "mousemove", function (e) { - $.data(document, "select2-lastpos", {x: e.pageX, y: e.pageY}); + lastMousePosition = {x: e.pageX, y: e.pageY}; }); /** @@ -174,7 +174,7 @@ */ function installFilteredMouseMove(element) { element.bind("mousemove", function (e) { - var lastpos = $.data(document, "select2-lastpos"); + var lastpos = lastMousePosition; if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) { $(e.target).trigger("mousemove-filtered", e); } From 3387853d9c4dbe8c44c268f47b255b159650f45d Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 12 Sep 2012 14:50:17 -0400 Subject: [PATCH 2/3] Keep a cached reference to jQueryfied document --- select2.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/select2.js b/select2.js index 35385cad..4f7ff4c6 100644 --- a/select2.js +++ b/select2.js @@ -40,7 +40,8 @@ return; } - var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer, lastMousePosition; + var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer, + lastMousePosition, $document; KEY = { TAB: 9, @@ -90,6 +91,8 @@ } }; + $document = $(document); + nextUid=(function() { var counter=1; return function() { return counter++; }; }()); function indexOf(value, array) { @@ -162,7 +165,7 @@ }); } - $(document).delegate("body", "mousemove", function (e) { + $document.delegate("body", "mousemove", function (e) { lastMousePosition = {x: e.pageX, y: e.pageY}; }); @@ -493,16 +496,16 @@ * * also takes care of clicks on label tags that point to the source element */ - $(document).ready(function () { - $(document).delegate("body", "mousedown touchend", function (e) { + $document.ready(function () { + $document.delegate("body", "mousedown touchend", function (e) { var target = $(e.target).closest("div.select2-container").get(0), attr; if (target) { - $(document).find("div.select2-container-active").each(function () { + $document.find("div.select2-container-active").each(function () { if (this !== target) $(this).data("select2").blur(); }); } else { target = $(e.target).closest("div.select2-drop").get(0); - $(document).find("div.select2-drop-active").each(function () { + $document.find("div.select2-drop-active").each(function () { if (this !== target) $(this).data("select2").blur(); }); } From 7bc63dd26447f11458ea047c5f663265d241a22a Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 12 Sep 2012 14:57:35 -0400 Subject: [PATCH 3/3] Get rid of 2 useless jQuery.delegate event handler --- select2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/select2.js b/select2.js index 4f7ff4c6..caa8660f 100644 --- a/select2.js +++ b/select2.js @@ -165,7 +165,7 @@ }); } - $document.delegate("body", "mousemove", function (e) { + $document.bind("mousemove", function (e) { lastMousePosition = {x: e.pageX, y: e.pageY}; }); @@ -497,7 +497,7 @@ * also takes care of clicks on label tags that point to the source element */ $document.ready(function () { - $document.delegate("body", "mousedown touchend", function (e) { + $document.bind("mousedown touchend", function (e) { var target = $(e.target).closest("div.select2-container").get(0), attr; if (target) { $document.find("div.select2-container-active").each(function () {