Improve performance of Web UI with very large user groups or any other usage which adds a lot of values into jQuery.ordered_map.
--
Petr Vobornik
From f11682039f41ce27f770637e67817b9ccdfad0af Mon Sep 17 00:00:00 2001
From: Petr Vobornik <pvobo...@redhat.com>
Date: Fri, 17 Apr 2015 16:12:21 +0200
Subject: [PATCH] jQuery.ordered_map: remove map attribute

map attribute is redundant and not used.

Use `get` method instead.
---
 install/ui/src/libs/jquery.ordered-map.js | 10 ++--------
 install/ui/test/ordered_map_tests.js      |  5 -----
 2 files changed, 2 insertions(+), 13 deletions(-)

diff --git a/install/ui/src/libs/jquery.ordered-map.js b/install/ui/src/libs/jquery.ordered-map.js
index 19f7b3dd5e2378fa38206fadda61fe4f06b71fd3..b25dc3379fc1c77e526bcdb9d177fb7e54ec7c48 100644
--- a/install/ui/src/libs/jquery.ordered-map.js
+++ b/install/ui/src/libs/jquery.ordered-map.js
@@ -30,11 +30,10 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
      */
     that.keys = [];
     that.values = [];
-    that.map = {};
     that.length = 0;
 
     that.get = function(key) {
-        return that.map[key];
+        return that.values[that._key_indicies[key]];
     };
 
     that.put = function(key, value, position) {
@@ -60,8 +59,6 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
             }
         }
 
-        that.map[key] = value;
-
         return that;
     };
 
@@ -111,11 +108,9 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
         var i = that.get_key_index(key);
         if (i<0) return null;
 
+        var value = that.values[i];
         that.keys.splice(i, 1);
         that.values.splice(i, 1);
-
-        var value = that.map[key];
-        delete that.map[key];
         delete that._key_indicies[key];
         that.length = that.keys.length;
         return value;
@@ -124,7 +119,6 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
     that.empty = function() {
         that.keys = [];
         that.values = [];
-        that.map = {};
         that._key_indicies = {};
         that.length = that.keys.length;
         return that;
diff --git a/install/ui/test/ordered_map_tests.js b/install/ui/test/ordered_map_tests.js
index f3d2d5b36aebea5ff3f4e7b6ddadb30a155b1156..f302de5d90de82e5967782de8e9f34af43d34a4f 100755
--- a/install/ui/test/ordered_map_tests.js
+++ b/install/ui/test/ordered_map_tests.js
@@ -30,7 +30,6 @@ test("Testing $.ordered_map constructor.", function() {
     strictEqual(test.length, 0, "Checking length.");
     deepEqual(test.keys, [], "Checking keys.");
     deepEqual(test.values, [], "Checking values.");
-    deepEqual(test.map, {}, "Checking map.");
 });
 
 test("Testing $.ordered_map.put().", function() {
@@ -84,7 +83,6 @@ test("Testing $.ordered_map.put().", function() {
     strictEqual(test.length, 8, 'Checking length.');
     deepEqual(test.keys, [key5, key4, key1, key3, key2, key6, key7, key8], 'Checking keys.');
     deepEqual(test.values, [value5, value4, value1, value3, value2, value6, value7, value8], 'Checking values.');
-    deepEqual(test.map, map, 'Checking map.');
 });
 
 test("Testing $.ordered_map.get().", function() {
@@ -110,7 +108,6 @@ test("Testing $.ordered_map.get().", function() {
     strictEqual(test.length, 2, 'Checking length.');
     deepEqual(test.keys, [key1, key2], 'Checking keys.');
     deepEqual(test.values, [value1, value2], 'Checking values.');
-    deepEqual(test.map, map, 'Checking map.');
     strictEqual(result1, value1, 'Checking result 1.');
     strictEqual(result2, value2, 'Checking result 2.');
 });
@@ -136,7 +133,6 @@ test("Testing $.ordered_map.remove().", function() {
     strictEqual(test.length, 1, 'Checking length.');
     deepEqual(test.keys, [key2], 'Checking keys.');
     deepEqual(test.values, [value2], 'Checking values.');
-    deepEqual(test.map, map, 'Checking map.');
     strictEqual(result1, value1, 'Checking result.');
 });
 
@@ -158,7 +154,6 @@ test("Testing $.ordered_map.empty().", function() {
     strictEqual(test.length, 0, 'Checking length.');
     deepEqual(test.keys, [], 'Checking keys.');
     deepEqual(test.values, [], 'Checking values.');
-    deepEqual(test.map, {}, 'Checking map.');
 });
 
 };});
\ No newline at end of file
-- 
2.1.0

From 001522023eef1c2f08c822297c8c0629f15c8e7d Mon Sep 17 00:00:00 2001
From: Petr Vobornik <pvobo...@redhat.com>
Date: Thu, 16 Apr 2015 18:54:25 +0200
Subject: [PATCH] jQuery.ordered_map: faster creation

Creation of map with e.g. 30K values was very slow. Map checked if a value is
in in the map but it used Array's indexOf method therefore the complexity was
quadratic instead of linear.
---
 install/ui/src/libs/jquery.ordered-map.js | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/install/ui/src/libs/jquery.ordered-map.js b/install/ui/src/libs/jquery.ordered-map.js
index 77d17c56ac926f1193c97e0cc46ceebe002005c6..19f7b3dd5e2378fa38206fadda61fe4f06b71fd3 100644
--- a/install/ui/src/libs/jquery.ordered-map.js
+++ b/install/ui/src/libs/jquery.ordered-map.js
@@ -22,6 +22,8 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
 
     var that = {};
 
+    that._key_indicies = {};
+
     /**
      * These variables can be read directly but should not be
      * modified directly. Use the provided methods instead.
@@ -46,12 +48,14 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
             if (typeof position !== 'number') {
                 that.keys.push(key);
                 that.values.push(value);
+                that._key_indicies[key] = that.keys.length -1;
                 that.length = that.keys.length;
             } else {
                 if (position < 0) position = 0;
                 else if (position > that.length) position = that.length;
                 that.keys.splice(position, 0, key);
                 that.values.splice(position, 0, value);
+                that._key_indicies[key] = position;
                 that.length = that.keys.length;
             }
         }
@@ -112,6 +116,7 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
 
         var value = that.map[key];
         delete that.map[key];
+        delete that._key_indicies[key];
         that.length = that.keys.length;
         return value;
     };
@@ -120,12 +125,13 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
         that.keys = [];
         that.values = [];
         that.map = {};
+        that._key_indicies = {};
         that.length = that.keys.length;
         return that;
     };
 
     that.get_key_index = function(key) {
-        return that.keys.indexOf(key);
+        return that._key_indicies[key];
     };
 
     that.get_key_by_index = function(index) {
-- 
2.1.0

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to