Skip to content

Commit

Permalink
perf(dot): optimize isAlphaNumeric (#1187)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Rasmus van der Burg and Thomaash authored Feb 6, 2021
1 parent 94e9297 commit 7dbd412
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/network/dotparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 7dbd412

Please sign in to comment.