Przeglądaj źródła

perf(ui): don't parse the keymap on every keypress

Every keypress triggers a loop over all registered shortcuts, and each
iteration calls `.split(" ")` on the combination string. With ~30 shortcuts
registered that's 30 `String.split()` allocations on every single keypress.
But since these results never change after `on()` is called, they can be
computed once there and then cached.
jvoisin 2 tygodni temu
rodzic
commit
873416f4be
1 zmienionych plików z 4 dodań i 5 usunięć
  1. 4 5
      internal/ui/static/js/keyboard_handler.js

+ 4 - 5
internal/ui/static/js/keyboard_handler.js

@@ -6,8 +6,9 @@ class KeyboardHandler {
     }
 
     on(combination, callback) {
-        this.shortcuts.set(combination, callback);
-        this.triggers.add(combination.split(" ")[0]);
+        const keys = combination.split(" ");
+        this.shortcuts.set(combination, { keys, callback });
+        this.triggers.add(keys[0]);
     }
 
     listen() {
@@ -23,9 +24,7 @@ class KeyboardHandler {
 
             this.queue.push(key);
 
-            for (const [combination, callback] of this.shortcuts.entries()) {
-                const keys = combination.split(" ");
-
+            for (const [combination, { keys, callback }] of this.shortcuts.entries()) {
                 if (keys.every((value, index) => value === this.queue[index])) {
                     this.queue = [];
                     callback(event);