Skip to content

Commit f83bdbd

Browse files
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.
1 parent eca598f commit f83bdbd

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ module.exports = function(css, options){
450450
*/
451451

452452
function atrule() {
453+
if (css[0] != '@') return;
454+
453455
return atkeyframes()
454456
|| atmedia()
455457
|| atsupports()
@@ -487,5 +489,5 @@ module.exports = function(css, options){
487489
*/
488490

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

0 commit comments

Comments
 (0)