Browse Source

get_tab_index(): use a for loop with early-exit for performance

Aarni Koskela 2 years ago
parent
commit
67d4360453
1 changed files with 6 additions and 8 deletions
  1. 6 8
      javascript/ui.js

+ 6 - 8
javascript/ui.js

@@ -100,15 +100,13 @@ function switch_to_extras() {
 }
 }
 
 
 function get_tab_index(tabId) {
 function get_tab_index(tabId) {
-    var res = 0;
-
-    gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i) {
-        if (button.className.indexOf('selected') != -1) {
-            res = i;
+    let buttons = gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button');
+    for (let i = 0; i < buttons.length; i++) {
+        if (buttons[i].classList.contains('selected')) {
+            return i;
         }
         }
-    });
-
-    return res;
+    }
+    return 0;
 }
 }
 
 
 function create_tab_index_args(tabId, args) {
 function create_tab_index_args(tabId, args) {