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

Optimize injectAndGetClassName #213

Merged
merged 1 commit into from
Mar 8, 2017

Conversation

lencioni
Copy link
Collaborator

@lencioni lencioni commented Mar 8, 2017

This function is called every time css() is used, so we want it to be as
fast as possible. I spotted three loops here that could be condensed
into one. In my profiling this seems to make the function about 30%
faster.

src/inject.js Outdated
injectStyleOnce(className, `.${className}`,
validDefinitions.map(d => d._definition),
useImportant, selectorHandlers);
const className = `${classNameBits.join("-o_O-")}`
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need to be in a template string, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oops, nope.

src/inject.js Outdated
const validDefinitions = styleDefinitions.filter((def) => def);

let classNameBits = [];
let definitionBits = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

These can be const, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh yes, of course.

src/inject.js Outdated
classNameBits.push(styleDefinitions[i]._name);
definitionBits.push(styleDefinitions[i]._definition);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: 4 space indents, please

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

gah! Would you be opposed to me setting up eslint to tell me about these things for this project?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hahaha not at all, I was thinking the same thing. We have eslint but it doesn't have much turned on. :P I think ideally it would follow the KA style eslint at https://github.com/Khan/khan-linter/blob/master/eslintrc but that's ginormous so maybe just adding in a few of the common annoying ones would be good...

This function is called every time css() is used, so we want it to be as
fast as possible. I spotted three loops here that could be condensed
into one. In my profiling this seems to make the function about 30%
faster.
@lencioni
Copy link
Collaborator Author

lencioni commented Mar 8, 2017

Updated!

Copy link
Contributor

@xymostech xymostech left a comment

Choose a reason for hiding this comment

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

LGTM!

@xymostech xymostech merged commit 1f23043 into Khan:master Mar 8, 2017
@xymostech
Copy link
Contributor

Mmkay, gonna go make the release now. Wish me luck!

@lencioni lencioni deleted the injectandgetclassname branch March 9, 2017 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants