Skip to content

Performance improvements to atrule() and trim() fns #55

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

Merged
merged 1 commit into from
Oct 23, 2013
Merged
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
Performance improvements to atrule() and trim() fns
Before this change, `atrule()` was a cascade of potentially failing RegExps.
This change introduces a short-circuit that bypasses the RegExps if the
next character is not an '@'. `make bench` shows a 20-25% improvement in
op/s.

Also before this change, when the argument is falsy `trim()` calls `replace()`
on the empty string. This change guards against unnecessary calls to `replace()`
with a ternary. `make bench` shows a 2.5-5.5% improvement in op/s.

With both changes, make bench shows a combined 30-40% improvement.

Benchmarks were run 5 times and the results averaged.
  • Loading branch information
brettstimmerman committed Oct 23, 2013
commit f83bdbdf122e9e0543fcfb865b0ea5fce32217a9
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ module.exports = function(css, options){
*/

function atrule() {
if (css[0] != '@') return;

return atkeyframes()
|| atmedia()
|| atsupports()
Expand Down Expand Up @@ -487,5 +489,5 @@ module.exports = function(css, options){
*/

function trim(str) {
return (str || '').replace(/^\s+|\s+$/g, '');
return str ? str.replace(/^\s+|\s+$/g, '') : '';
}