From 3f0699a949b029994acf79bc6f927c9901166c11 Mon Sep 17 00:00:00 2001 From: Adrian Hughes Date: Fri, 9 Apr 2021 13:36:32 +0200 Subject: [PATCH] Optimize applyFilter() to Avoid Redundant String Operations (#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. --- .../contributions/ui/FilterJTextField.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/src/cc/arduino/contributions/ui/FilterJTextField.java b/app/src/cc/arduino/contributions/ui/FilterJTextField.java index f4cb34203..83aeba454 100644 --- a/app/src/cc/arduino/contributions/ui/FilterJTextField.java +++ b/app/src/cc/arduino/contributions/ui/FilterJTextField.java @@ -101,13 +101,16 @@ public class FilterJTextField extends JTextField { } public void applyFilter() { - String filter = showingHint ? "" : getText(); - filter = filter.toLowerCase(); - - // Replace anything but 0-9, a-z, or : with a space - filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " "); - - onFilter(filter.split(" ")); + String[] filteredText = new String[0]; + if (!showingHint) { + String filter = getText().toLowerCase(); + + // Replace anything but 0-9, a-z, or : with a space + filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " "); + + filteredText = filter.split(" "); + } + onFilter(filteredText); } protected void onFilter(String[] strings) {