From 7dbd412406eec9b658116c601c145afb41ee7d16 Mon Sep 17 00:00:00 2001 From: Rasmus van der Burg Date: Sat, 6 Feb 2021 19:34:41 +0100 Subject: [PATCH] perf(dot): optimize isAlphaNumeric (#1187) * Optimized isAlphaNumeric Optimized isAlphaNumeric to use charcodes instead if regex. This gives the function a worst case 60%+ performance on V8 and 40%+ on Spidermonkey * Fix linting issues * Fixed linting Co-authored-by: Tomina --- lib/network/dotparser.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/network/dotparser.js b/lib/network/dotparser.js index 758c3131fc..90c7417ce3 100644 --- a/lib/network/dotparser.js +++ b/lib/network/dotparser.js @@ -115,15 +115,37 @@ function nextPreview() { return dot.charAt(index + 1); } -var regexAlphaNumeric = /[a-zA-Z_0-9.:#]/; /** - * Test whether given character is alphabetic or numeric + * Test whether given character is alphabetic or numeric ( a-zA-Z_0-9.:# ) * * @param {string} c * @returns {boolean} isAlphaNumeric */ function isAlphaNumeric(c) { - return regexAlphaNumeric.test(c); + var charCode = c.charCodeAt(0); + + if (charCode < 47) { + // #. + return charCode === 35 || charCode === 46; + } + if (charCode < 59) { + // 0-9 and : + return charCode > 47; + } + if (charCode < 91) { + // A-Z + return charCode > 64; + } + if (charCode < 96) { + // _ + return charCode === 95; + } + if (charCode < 123) { + // a-z + return charCode > 96; + } + + return false; } /**