Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
importantify()
performance (#201)
* Avoid unused capture group in IMPORTANT_RE I was rolling through the code here looking for some opportunities to speed it up and I noticed that there is a capture group in this regex that isn't being used. By changing `(` to `(?:` we can avoid capturing this group while still being able to use the parens for grouping. * Use string for importantify replace I was curious about the performance characteristics of various options here, and using a string in this way seems to have the edge, at least in Firefox (35% faster) and Safari (12% faster)--Chrome performance seems pretty similar for all possibilities. I also find this version more readable, so it seems like we may as well go with this option. https://jsperf.com/replace-string-vs-function/ * Simplify importantify regex I don't think we actually need a capture group at all here and can accomplish the same thing by just anchoring on the end of the string. This reduces the runtime of this function in my benchmark from ~105ms to ~35ms. * Scope importantify to just the value Since we only call this function from one place, and we already have the CSS property name and value split out, we can further optimize this hot path by only running importantify on the CSS property value. In my benchmark, this change speeds up this map by ~22% (~128ms to ~100ms). * Remove regex from importantify Now that this has been sufficiently simplified, we can easily remove the regex entirely. * Add bracket optimization to importantify In our microbenchmark, adding this optimization performs a good amount better. Since it is a simple optimization to add, it seems like we may as well do it. https://jsperf.com/slice-vs-bracket-string-check-with-imporant I considered using bracket notation for the whole check, but I decided that was too verbose. So instead, I decided to just check for one character and follow up with a more thorough but concise check afterward. This optimization favors the case where the style does not already have `!important` on it, which is by and large likely to be the general case. * Move conditional out of loop in generateCSSRuleset This cleans up the code a bit, and is also faster! https://jsperf.com/if-vs-extra-function/1
- Loading branch information