Skip to content

Commit 5e54e26

Browse files
authored
(perf) Add comments to blanked script/style tags (#654)
Svelte's parse method is very slow when scripts contain lots of whitespace. Instead of changing the regex in svelte, we just stop making too much white space by introducing /**/ comments at the end of lines when blanking out the script tag. #253
1 parent 2dfb6e8 commit 5e54e26

File tree

3 files changed

+2295
-4
lines changed

3 files changed

+2295
-4
lines changed

packages/svelte2tsx/src/utils/htmlxparser.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
8282
for (const node of verbatimElements) {
8383
const content = node.content;
8484
if (content) {
85-
output =
86-
output.substring(0, content.start) +
87-
output.substring(content.start, content.end).replace(/[^\n]/g, ' ') +
88-
output.substring(content.end);
85+
output = htmlx.substring(0, content.start) +
86+
htmlx.substring(content.start, content.end)
87+
// blank out the content
88+
.replace(/[^\n]/g, ' ')
89+
// excess blank space can make the svelte parser very slow (sec->min). break it up with comments (works in style/script)
90+
.replace(/[^\n][^\n][^\n][^\n]\n/g, '/**/\n') +
91+
htmlx.substring(content.end);
8992
}
9093
}
9194
return output;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let converter = require('../build/htmlxtojsx')
2+
let fs = require('fs')
3+
let assert = require('assert')
4+
5+
describe('htmlxparser', () => {
6+
let content = fs.readFileSync(`${__dirname}/large.svelte`, {encoding: 'utf8'});
7+
8+
it('parses in a reasonable time', () => {
9+
const start = new Date();
10+
converter.htmlx2jsx(content);
11+
const elapsed = new Date() - start;
12+
assert(elapsed <= 1000, `Parsing took ${elapsed} ms, which was longer than 1000ms`);
13+
})
14+
15+
});

0 commit comments

Comments
 (0)