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

Improve importantify() performance #201

Merged
merged 7 commits into from
Mar 7, 2017
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
12 changes: 7 additions & 5 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,13 @@ export const generateCSSRuleset = (
})
);

const rules = prefixedRules.map(([key, value]) => {
const stringValue = stringifyValue(key, value);
const ret = `${kebabifyStyleName(key)}:${stringValue};`;
return useImportant === false ? ret : importantify(ret);
}).join("");
const transformValue = (useImportant === false)
? stringifyValue
: (key, value) => importantify(stringifyValue(key, value));

const rules = prefixedRules
.map(([key, value]) => `${kebabifyStyleName(key)}:${transformValue(key, value)};`)
.join("");

if (rules) {
return `${selector}{${rules}}`;
Expand Down
20 changes: 12 additions & 8 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,15 @@ function murmurhash2_32_gc(str) {
export const hashObject = (object /* : ObjectMap */) /* : string */ => murmurhash2_32_gc(JSON.stringify(object));


const IMPORTANT_RE = /^([^:]+:.*?)( !important)?;$/;

// Given a single style rule string like "a: b;", adds !important to generate
// "a: b !important;".
export const importantify = (string /* : string */) /* : string */ =>
string.replace(
IMPORTANT_RE,
(_, base) => base + " !important;");
// Given a single style value string like the "b" from "a: b;", adds !important
// to generate "b !important".
export const importantify = (string /* : string */) /* : string */ => (
// Bracket string character access is very fast, and in the default case we
// normally don't expect there to be "!important" at the end of the string
// so we can use this simple check to take an optimized path. If there
// happens to be a "!" in this position, we follow up with a more thorough
// check.
(string[string.length - 10] === '!' && string.slice(-11) === ' !important')
? string
: `${string} !important`
);