You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On modern runtimes, testing identifiers for matches with keywords is significantly (up to 60%) faster by using Set than by using a RegExp.
The lookup is faster still if you first check the length of the identifier to avoid computing hashcodes for strings that are too long or too short to match.
Sample code:
functioncreateTester(words){letminLength=Infinity;letmaxLength=0;constsplit=words.split(' ');constset=newSet();for(leti=0;i<split.length;i++){constword=split[i];set.add(word);if(word.length<minLength)minLength=word.length;if(word.length>maxLength)maxLength=word.length;}returnregexpCache[words]={test: function(input){// Length testing the string is cheaper than computing the hash code for long stringsreturninput.length>=minLength&&input.length<=maxLength&&set.has(input)}}}exportfunctionwordsRegexp(words){returnregexpCache[words]||createTester(words)}
Existing implementation:
Impact of suggested implementation:
The text was updated successfully, but these errors were encountered:
On modern runtimes, testing identifiers for matches with keywords is significantly (up to 60%) faster by using Set than by using a RegExp.
The lookup is faster still if you first check the length of the identifier to avoid computing hashcodes for strings that are too long or too short to match.
Sample code:
Existing implementation:
Impact of suggested implementation:
The text was updated successfully, but these errors were encountered: