-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
potentially speed up hyphenating style names #3251
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,10 @@ | |
* https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/hyphenateStyleName.js | ||
*/ | ||
|
||
const uppercasePattern = /([A-Z])/g; | ||
const uppercaseCheck = /([A-Z])/; | ||
const uppercasePattern = new RegExp(uppercaseCheck, 'g'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a quick note here for a minor change; with gzipping it's likely that this will end up being smaller if we just repeat the pattern (with the added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, my original attempt also duplicated the regex, so if that's preferable I can put together a PR. I have no idea if JS engines treat |
||
const msPattern = /^ms-/; | ||
const prefixAndLowerCase = (char: string): string => `-${char.toLowerCase()}`; | ||
|
||
/** | ||
* Hyphenates a camelcased CSS property name, for example: | ||
|
@@ -25,8 +27,9 @@ const msPattern = /^ms-/; | |
* @return {string} | ||
*/ | ||
export default function hyphenateStyleName(string: string): string { | ||
return string | ||
.replace(uppercasePattern, '-$1') | ||
.toLowerCase() | ||
.replace(msPattern, '-ms-'); | ||
return uppercaseCheck.test(string) | ||
? string | ||
.replace(uppercasePattern, prefixAndLowerCase) | ||
.replace(msPattern, '-ms-') | ||
: string; | ||
} |
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.
I don't understand why this change would be meaningfully 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.
For performance reasons it's using
Regexp.prototype.test
(as opposed toString.prototype.match
) and quoting MDN:It's a little unclear how it works so I've always avoided it - is each regex storing which strings it's been matched against?
Or is it a per-regex counter for all strings it's matched against?
This seems to imply it's a per-regex counter, so by using a non-global version, the test check isn't internally changing its state.
As further proof of this, changing
uppercaseCheck
to/([A-Z])/g
causes a load of tests to start failing as some names stop becoming hyphenated!(If there's a better way to solve this problem I'm happy to change it)
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.
Hmm, fair enough.