From e6bf54d27bfe2270ba4087d9c554d49848daf1e9 Mon Sep 17 00:00:00 2001 From: inventivetalent Date: Thu, 15 Nov 2018 17:55:59 +0100 Subject: [PATCH] add options to use Array.filter instead of loop & limit result size, to improve search performance --- src/litegraph.js | 65 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/litegraph.js b/src/litegraph.js index 953949d72..38bcd39ca 100755 --- a/src/litegraph.js +++ b/src/litegraph.js @@ -6660,6 +6660,9 @@ LGraphCanvas.prototype.prompt = function( title, value, callback, event ) return dialog; } + +LGraphCanvas.search_filter = false; +LGraphCanvas.search_limit = -1; LGraphCanvas.prototype.showSearchBox = function(event) { var that = this; @@ -6788,30 +6791,48 @@ LGraphCanvas.prototype.showSearchBox = function(event) selected.scrollIntoView(); } - function refreshHelper() - { - timeout = null; - var str = input.value; - first = null; - helper.innerHTML = ""; - if(!str) - return; + function refreshHelper() { + timeout = null; + var str = input.value; + first = null; + helper.innerHTML = ""; + if (!str) + return; - if( that.onSearchBox ) - that.onSearchBox( help, str, graphcanvas ); - else - for( var i in LiteGraph.registered_node_types ) - if(i.indexOf(str) != -1) - { - var help = document.createElement("div"); - if(!first) first = i; - help.innerText = i; - help.className = "litegraph lite-search-item"; - help.addEventListener("click", function(e){ - select( this.innerText ); - }); - helper.appendChild(help); + if (that.onSearchBox){ + that.onSearchBox(help, str, graphcanvas); + } else { + function addResult(result) { + var help = document.createElement("div"); + if (!first) first = result; + help.innerText = result; + help.className = "litegraph lite-search-item"; + help.addEventListener("click", function (e) { + select(this.innerText); + }); + helper.appendChild(help); + } + let c = 0; + if(LGraphCanvas.search_filter) { + str = str.toLowerCase(); + + var keys = Object.keys(LiteGraph.registered_node_types); + var filtered = keys.filter(function (item) { + return item.toLowerCase().indexOf(str) !== -1; + }); + for(var i = 0; i < filtered.length; i++) { + addResult(filtered[i]); + if(LGraphCanvas.search_limit !== -1 && c++ > LGraphCanvas.search_limit) break; } + } else { + for (var i in LiteGraph.registered_node_types) { + if (i.indexOf(str) != -1) { + addResult(i); + if(LGraphCanvas.search_limit !== -1 && c++ > LGraphCanvas.search_limit) break; + } + } + } + } } return dialog;