-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Faster SSR #5701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faster SSR #5701
Changes from 7 commits
aa15e9a
e58d658
57bb045
29d4695
3a94f2a
bc4bda9
30df740
88e2146
1b0e836
61e180b
b32049b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,20 +69,35 @@ export function merge_ssr_styles(style_attribute, style_directive) { | |
return style_object; | ||
} | ||
|
||
export const escaped = { | ||
const ATTR_REGEX = /[&"]/g; | ||
const CONTENT_REGEX = /[&<]/g; | ||
|
||
const escapes = { | ||
'"': '"', | ||
"'": ''', | ||
'&': '&', | ||
'<': '<', | ||
'>': '>' | ||
'<': '<' | ||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export function escape(html) { | ||
return String(html).replace(/["'&<>]/g, match => escaped[match]); | ||
export function escape(html: string, is_attr = false) { | ||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (typeof html !== 'string') return html; | ||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX; | ||
pattern.lastIndex = 0; | ||
|
||
let escaped = ''; | ||
let last = 0; | ||
|
||
while (pattern.test(html)) { | ||
const i = pattern.lastIndex - 1; | ||
escaped += html.slice(last, i) + escapes[html[i]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for what it's worth, replacing but i'm surprised the regexp performs so badly; interestingly enough, it's the function as second parameter that's the slowdown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow. Maybe we should just do that instead then? How are you getting those numbers, and can you share them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just took a 500 kb HTML document and ran
Our documents of course tend to be smaller so there is a risk this doesn't correspond to real-life performance. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my testing a while (match = matchHtmlRegExp.exec(str)) {
switch (str.charCodeAt(match.index)) {
case 34: // "
escape = '"'
break I forget but it was something like 240 ops/sec vs 280 ops/sec (on a large file). I just submitted a 30% speed improvement patch to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe this is broken and it needs to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Another thing we have to keep in mind here is that almost 2 years have passed. It's very hard IMHO to micro-tune JS performance (over the long-term). Engines vary, browsers vary, things change with time. The thing that was fastest 2 years ago might bench worse today. The regex engines used in the major browsers are not all created equal. We should be clear if we're talking benchmarks which browser, which engine... in the Svelt context perhaps we only care about Node? Just a consideration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I tested on Node. This is for server-side code, so no need to test in browsers. Thanks for sharing your findings! It may well have been different in the past like you said and regardless it was valuable to test various ideas and ensure we're doing as well as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh for sure... we can certainly chase the peak performance (and probably should)... but we just may have to do so again in another 2 years - and make sure we're always comparing apples to apples. :). If we only need to test on Node that certainly helps a lot. Though it might not surprise me to learn if there were say differences between Node on ARM vs Node on x86_64... |
||
last = i + 1; | ||
} | ||
|
||
return escaped + html.slice(last); | ||
} | ||
|
||
export function escape_attribute_value(value) { | ||
return typeof value === 'string' ? escape(value) : value; | ||
return typeof value === 'string' ? escape(value, true) : value; | ||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export function escape_object(obj) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.