Skip to content

Commit 3f0699a

Browse files
Optimize applyFilter() to Avoid Redundant String Operations (arduino#11284)
if 'showingHint' == true, then many potentially expensive String operations are being executed on an empty string. This change wraps these operations in an if block which will only run them when needed.
1 parent f4f98cf commit 3f0699a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

app/src/cc/arduino/contributions/ui/FilterJTextField.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ private void spawnTimer() {
101101
}
102102

103103
public void applyFilter() {
104-
String filter = showingHint ? "" : getText();
105-
filter = filter.toLowerCase();
106-
107-
// Replace anything but 0-9, a-z, or : with a space
108-
filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " ");
109-
110-
onFilter(filter.split(" "));
104+
String[] filteredText = new String[0];
105+
if (!showingHint) {
106+
String filter = getText().toLowerCase();
107+
108+
// Replace anything but 0-9, a-z, or : with a space
109+
filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " ");
110+
111+
filteredText = filter.split(" ");
112+
}
113+
onFilter(filteredText);
111114
}
112115

113116
protected void onFilter(String[] strings) {

0 commit comments

Comments
 (0)