fix: Template export { x } stuck in infinite loop#15534
fix: Template export { x } stuck in infinite loop#15534liuxingbaoyu merged 1 commit intobabel:mainfrom
export { x } stuck in infinite loop#15534Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54203/ |
37b4f08 to
0597df6
Compare
| let nameSet: Set<string>; | ||
| let metadata; | ||
| let prefix = ""; | ||
| let prefix = "BABEL_TPL$"; |
There was a problem hiding this comment.
If we are always adding enough $s anyway, why do we need a more complex prefix?
There was a problem hiding this comment.
Just to reduce the possibility of duplication.
| : [], | ||
| ), | ||
| prefix = "$$" + prefix; | ||
| } while (raw.includes(prefix)); |
There was a problem hiding this comment.
Instead of trying until we add enough $s, can we instead do this and only compute it once?
let $count = 0;
const re = /\$+(?=\d)/g;
let m;
while (m = re.exec(tpl)) $count = Math.max($count, m[0].length);
const prefix = "$".repeat($count + 1);There was a problem hiding this comment.
I guess includes() would be a bit faster? Of course it doesn't matter.
There was a problem hiding this comment.
The difference is that with includes we iterate over the string n times (where "n" is the final number of times we have to expand the prefix), while with the regexp loop only once (because the regexp alwags start after the previous match, and matches prefixes of any length).
There was a problem hiding this comment.
It seems that only in the last case (very long code and $$$$$$0) the regex is slightly faster, while in other cases includes() is faster.
It should be faster to use BABEL_TPL$ as per the current code.
perf
JLHwung
left a comment
There was a problem hiding this comment.
I am not very familiar with babel template, rubberstamp lgtm.
Thanks @await-ovo!