-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tiny performance improvement by not compiling regular expression each time #3479
Conversation
lowerCase.js
Outdated
@@ -19,8 +19,9 @@ import words from './words.js' | |||
* lowerCase('__FOO_BAR__') | |||
* // => 'foo bar' | |||
*/ | |||
const rx = /['\u2019]/g |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nazar-pc Just curious, how does this effect the performance?
Im assuming that defining the regex before using it will eliminate the need for js to create a reference to it dynamically...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regular expressions take time to compile and additional time to execute upon some data. If you call lowerCase in a loop, regular expression will be compiled again and again on each call.
With proposed change it will be compiler once, thus each iteration will only involve executing pre-compiled regular expression, which is much faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename the var to reQuotes
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (commit amended)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhh cool, thank you!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
New version is constantly faster both in Firefox Nightly and Chromium Nightly: https://jsfiddle.net/dzym8svy/
Some projects call this function very often and performance hit can be significant in that case.