@samitouri / QOS-React-2 / commits / 522f47345f

Move the Error creation to be lazy (#24728)

Creating an Error captures a stack trace which can be somewhat expensive. We shouldn't do tthat always for every render. This also ensures that the stack trace is more useful because you can follow through the Node.js code to see the cause.

Sebastian Markbåge committed Jun 14, 2022 at 20:16 UTC 522f47345f79bb117f338384e75c8a79622bd735
1 file changed +6 -10
packages/react-dom/src/server/ReactDOMFizzServerNode.js
+6 -10
@@ -29,7 +29,8 @@ function createDrainHandler(destination, request) {
29 }
30
31 function createAbortHandler(request, reason) {
32 - return () => abort(request, reason);
32 + // eslint-disable-next-line react-internal/prod-error-codes
33 + return () => abort(request, new Error(reason));
34 }
35
36 type Options = {|
@@ -49,7 +50,7 @@ type Options = {|
50 type PipeableStream = {|
51 // Cancel any pending I/O and put anything remaining into
52 // client rendered mode.
52 - abort(): void,
53 + abort(reason: mixed): void,
54 pipe<T: Writable>(destination: T): T,
55 |};
56
@@ -94,21 +95,16 @@ function renderToPipeableStream(
95 'error',
96 createAbortHandler(
97 request,
97 - // eslint-disable-next-line react-internal/prod-error-codes
98 - new Error('The destination stream errored while writing data.'),
98 + 'The destination stream errored while writing data.',
99 ),
100 );
101 destination.on(
102 'close',
103 - createAbortHandler(
104 - request,
105 - // eslint-disable-next-line react-internal/prod-error-codes
106 - new Error('The destination stream closed early.'),
107 - ),
103 + createAbortHandler(request, 'The destination stream closed early.'),
104 );
105 return destination;
106 },
111 - abort(reason) {
107 + abort(reason: mixed) {
108 abort(request, reason);
109 },
110 };