From 14f34b324b2845c3218e2615db15ee9c818a809d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Schr=C3=B6der?= Date: Tue, 7 May 2024 15:23:48 +0200 Subject: [PATCH] Performance fix - compile pattern only once and reuse From java doc java.util.regex.Pattern.matches "If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time." --- .../src/main/java/com/devskiller/friendly_id/Base62.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/friendly-id/src/main/java/com/devskiller/friendly_id/Base62.java b/friendly-id/src/main/java/com/devskiller/friendly_id/Base62.java index c6a1c66..ada4f61 100644 --- a/friendly-id/src/main/java/com/devskiller/friendly_id/Base62.java +++ b/friendly-id/src/main/java/com/devskiller/friendly_id/Base62.java @@ -18,6 +18,7 @@ class Base62 { private static final BigInteger BASE = BigInteger.valueOf(62); private static final String DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + private static final Pattern DIGITS_PATTERN = Pattern.compile("[" + DIGITS + "]*"); /** * Encodes a number using Base62 encoding. @@ -63,7 +64,7 @@ static BigInteger decode(final String string, int bitLimit) { return throwIllegalArgumentException("String '%s' must not be empty", string); } - if (!Pattern.matches("[" + DIGITS + "]*", string)) { + if (!DIGITS_PATTERN.matcher(string).matches()) { throwIllegalArgumentException("String '%s' contains illegal characters, only '%s' are allowed", string, DIGITS); }