Browse Source

Update completion reply merge to reduce complexity from O(n^2) to O(n).

Afshin Darian 6 years ago
parent
commit
a00c7e1209
1 changed files with 12 additions and 3 deletions
  1. 12 3
      packages/completer/src/connector.ts

+ 12 - 3
packages/completer/src/connector.ts

@@ -76,11 +76,20 @@ namespace Private {
       return kernel;
     }
 
-    // They both have matches, so merge them, with a preference for the
-    // kernel result.
+    // They both have matches, merge them with a preference for the kernel
+    // result. Both lists are known to contain unique, non-repeating items,
+    // so return a non-repeating superset by filtering out duplicates from
+    // the context list that appear in the kernel list.
     let matches = kernel.matches.slice();
+    let memo = matches.reduce(
+      (acc, val) => {
+        acc[val] = val;
+        return acc;
+      },
+      {} as { [key: string]: string }
+    );
     context.matches.forEach(match => {
-      if (matches.indexOf(match) === -1) {
+      if (!(match in memo)) {
         matches.push(match);
       }
     });