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

Small lints #3244

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/curly-jars-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@emotion/serialize': patch
'@emotion/utils': patch
---

Minor performance improvement
2 changes: 1 addition & 1 deletion packages/serialize/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function createStringFromObject(
return string
}

let labelPattern = /label:\s*([^\s;\n{]+)\s*(;|$)/g
let labelPattern = /label:\s*([^\s;{]+)\s*(;|$)/g
Copy link
Contributor Author

Choose a reason for hiding this comment

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

\s contains \n. See node -p "'\\n'.match(/\\s/)"


let sourceMapPattern: RegExp | undefined
if (isDevelopment) {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getRegisteredStyles(
classNames.split(' ').forEach(className => {
if (registered[className] !== undefined) {
registeredStyles.push(`${registered[className]};`)
} else {
} else if (className) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

classNames could have multiple sequential spaces, in which case .split(' ') would produce empty strings.

Copy link
Member

Choose a reason for hiding this comment

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

a test case probably could be written for this, does it create a problem though? a rawClassName has already been allocated, can't this use some kind of string ropes or something to stay optimized?

Copy link
Contributor Author

@romgrk romgrk Aug 22, 2024

Choose a reason for hiding this comment

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

Yeah someone might have inserted additional whitespaces in their classnames.

So sure engines will usually use a concatenated string like I describe here to optimize concat operations, but adding a single ' ' is likely too small to be worth it, engines are most likely just allocating a new string for those bytes.

I wrote a benchmark with only this difference, it does seem to have a positive impact: (I inserted a bunch of spaces to highlight the impact more clearly)

image

rawClassName += `${className} `
}
})
Expand Down
Loading