Skip to content

Commit

Permalink
Minor performance optimization
Browse files Browse the repository at this point in the history
* Minor performance optimization for at rule check.
  • Loading branch information
puckowski committed Jul 21, 2023
1 parent 7a4d9a8 commit b649e5c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 20 deletions.
18 changes: 13 additions & 5 deletions dist/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,12 @@
skipWhitespace(1);
return tok;
};
parserInput.$peekChar = function (tok) {
if (input.charAt(parserInput.i) !== tok) {
return null;
}
return tok;
};
parserInput.$str = function (tok) {
var tokLength = tok.length;
// https://jsperf.com/string-startswith/21
Expand Down Expand Up @@ -4910,11 +4916,13 @@
debugInfo = getDebugInfo(index);
}
parserInput.save();
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}
if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
if (parserInput.$peekChar('@')) {
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}
if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
}
}
parserInput.restore();
},
Expand Down
2 changes: 1 addition & 1 deletion dist/less.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/less.min.js.map

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions packages/less/dist/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,12 @@
skipWhitespace(1);
return tok;
};
parserInput.$peekChar = function (tok) {
if (input.charAt(parserInput.i) !== tok) {
return null;
}
return tok;
};
parserInput.$str = function (tok) {
var tokLength = tok.length;
// https://jsperf.com/string-startswith/21
Expand Down Expand Up @@ -4910,11 +4916,13 @@
debugInfo = getDebugInfo(index);
}
parserInput.save();
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}
if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
if (parserInput.$peekChar('@')) {
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}
if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
}
}
parserInput.restore();
},
Expand Down
2 changes: 1 addition & 1 deletion packages/less/dist/less.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/less/dist/less.min.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/less/src/less/parser/parser-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ export default () => {
return tok;
};

parserInput.$peekChar = tok => {
if (input.charAt(parserInput.i) !== tok) {
return null;
}
return tok;
};

parserInput.$str = tok => {
const tokLength = tok.length;

Expand Down
14 changes: 8 additions & 6 deletions packages/less/src/less/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1845,12 +1845,14 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
}
parserInput.save();

if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}

if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
if (parserInput.$peekChar('@')) {
if (parserInput.$str('@media')) {
return this.prepareAndGetNestableAtRule(tree.Media, index, debugInfo, MediaSyntaxOptions);
}

if (parserInput.$str('@container')) {
return this.prepareAndGetNestableAtRule(tree.Container, index, debugInfo, ContainerSyntaxOptions);
}
}

parserInput.restore();
Expand Down

0 comments on commit b649e5c

Please # to comment.