Skip to content

Commit 8c85b02

Browse files
authored
[Fizz] Optimize end tags chunks (facebook#27522)
Implements `endChunkForTag` to make writing end tags faster
1 parent d803f51 commit 8c85b02

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -2509,7 +2509,7 @@ function pushStyleImpl(
25092509
target.push(stringToChunk(escapeTextForBrowser('' + child)));
25102510
}
25112511
pushInnerHTML(target, innerHTML, children);
2512-
target.push(endTag1, stringToChunk('style'), endTag2);
2512+
target.push(endChunkForTag('style'));
25132513
return null;
25142514
}
25152515

@@ -2824,7 +2824,7 @@ function pushTitleImpl(
28242824
target.push(stringToChunk(escapeTextForBrowser('' + child)));
28252825
}
28262826
pushInnerHTML(target, innerHTML, children);
2827-
target.push(endTag1, stringToChunk('title'), endTag2);
2827+
target.push(endChunkForTag('title'));
28282828
return null;
28292829
}
28302830

@@ -3081,7 +3081,7 @@ function pushScriptImpl(
30813081
if (typeof children === 'string') {
30823082
target.push(stringToChunk(encodeHTMLTextNode(children)));
30833083
}
3084-
target.push(endTag1, stringToChunk('script'), endTag2);
3084+
target.push(endChunkForTag('script'));
30853085
return null;
30863086
}
30873087

@@ -3484,8 +3484,15 @@ export function pushStartInstance(
34843484
return pushStartGenericElement(target, props, type);
34853485
}
34863486

3487-
const endTag1 = stringToPrecomputedChunk('</');
3488-
const endTag2 = stringToPrecomputedChunk('>');
3487+
const endTagCache = new Map<string, PrecomputedChunk>();
3488+
function endChunkForTag(tag: string): PrecomputedChunk {
3489+
let chunk = endTagCache.get(tag);
3490+
if (chunk === undefined) {
3491+
chunk = stringToPrecomputedChunk('</' + tag + '>');
3492+
endTagCache.set(tag, chunk);
3493+
}
3494+
return chunk;
3495+
}
34893496

34903497
export function pushEndInstance(
34913498
target: Array<Chunk | PrecomputedChunk>,
@@ -3547,7 +3554,7 @@ export function pushEndInstance(
35473554
}
35483555
break;
35493556
}
3550-
target.push(endTag1, stringToChunk(type), endTag2);
3557+
target.push(endChunkForTag(type));
35513558
}
35523559

35533560
function writeBootstrap(
@@ -4502,9 +4509,7 @@ export function writePreamble(
45024509
// if the main content contained the </head> it would also have provided a
45034510
// <head>. This means that all the content inside <html> is either <body> or
45044511
// invalid HTML
4505-
writeChunk(destination, endTag1);
4506-
writeChunk(destination, stringToChunk('head'));
4507-
writeChunk(destination, endTag2);
4512+
writeChunk(destination, endChunkForTag('head'));
45084513
}
45094514
}
45104515

@@ -4577,14 +4582,10 @@ export function writePostamble(
45774582
resumableState: ResumableState,
45784583
): void {
45794584
if (resumableState.hasBody) {
4580-
writeChunk(destination, endTag1);
4581-
writeChunk(destination, stringToChunk('body'));
4582-
writeChunk(destination, endTag2);
4585+
writeChunk(destination, endChunkForTag('body'));
45834586
}
45844587
if (resumableState.hasHtml) {
4585-
writeChunk(destination, endTag1);
4586-
writeChunk(destination, stringToChunk('html'));
4587-
writeChunk(destination, endTag2);
4588+
writeChunk(destination, endChunkForTag('html'));
45884589
}
45894590
}
45904591

0 commit comments

Comments
 (0)