Skip to content

Commit

Permalink
(perf) Add comments to blanked script/style tags (#654)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
halfnelson committed Nov 5, 2020
1 parent 2dfb6e8 commit 5e54e26
Show file tree
Hide file tree
Showing 3 changed files with 2,295 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/svelte2tsx/src/utils/htmlxparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
for (const node of verbatimElements) {
const content = node.content;
if (content) {
output =
output.substring(0, content.start) +
output.substring(content.start, content.end).replace(/[^\n]/g, ' ') +
output.substring(content.end);
output = htmlx.substring(0, content.start) +
htmlx.substring(content.start, content.end)
// blank out the content
.replace(/[^\n]/g, ' ')
// excess blank space can make the svelte parser very slow (sec->min). break it up with comments (works in style/script)
.replace(/[^\n][^\n][^\n][^\n]\n/g, '/**/\n') +
htmlx.substring(content.end);
}
}
return output;
Expand Down
15 changes: 15 additions & 0 deletions packages/svelte2tsx/test/htmlxparser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let converter = require('../build/htmlxtojsx')
let fs = require('fs')
let assert = require('assert')

describe('htmlxparser', () => {
let content = fs.readFileSync(`${__dirname}/large.svelte`, {encoding: 'utf8'});

it('parses in a reasonable time', () => {
const start = new Date();
converter.htmlx2jsx(content);
const elapsed = new Date() - start;
assert(elapsed <= 1000, `Parsing took ${elapsed} ms, which was longer than 1000ms`);
})

});
Loading

0 comments on commit 5e54e26

Please sign in to comment.