Skip to content
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

Merged
merged 1 commit into from
Nov 7, 2017
Merged

Conversation

nazar-pc
Copy link
Contributor

@nazar-pc nazar-pc commented Nov 7, 2017

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.

@jsf-clabot
Copy link

jsf-clabot commented Nov 7, 2017

CLA assistant check
All committers have signed the CLA.

lowerCase.js Outdated
@@ -19,8 +19,9 @@ import words from './words.js'
* lowerCase('__FOO_BAR__')
* // => 'foo bar'
*/
const rx = /['\u2019]/g

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...

Copy link
Contributor Author

@nazar-pc nazar-pc Nov 7, 2017

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (commit amended)

Copy link

@elevenpassin elevenpassin Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh cool, thank you!

@lock
Copy link

lock bot commented Dec 26, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

4 participants