Skip to content

Commit 28f111a

Browse files
committed
perf(@angular/ssr): optimized request handling performance
This commit refactors request handling logic, leading to significant performance improvements. **Benchmark** | Metric | Before Optimization | After Optimization | Improvement | |---------------|----------------------|----------------------|-------------| | Latency (Avg) | 2655.35 ms | 2385.85 ms | ~10.1% | | Latency (50%) | 2615 ms | 2416.33 ms | ~7.6% | | Req/Sec (Avg) | 364.54 | 400.12 | ~9.8% | | Bytes/Sec (Avg)| 7.7 MB | 8.45 MB | ~9.7% | | Requests | 12k | 13k | ~8.3% | | Errors | 10 (timeouts) | 0 | 100% | **Test Details:** * **Command:** `npx autocannon -c 100 -d 30 -p 10 http://localhost:<port>` * **Parameters:** * `-c 100`: 100 concurrent connections * `-d 30`: 30 seconds duration * `-p 10`: 10 pipelining factor * **Iterations:** 3 tests were run before optimization, and 3 tests were run after optimization. Average of each set of tests was used for the comparison. * **Samples:** 30 samples were collected per test run for Req/Bytes counts. The optimized request handling logic (After Optimization) demonstrates substantial improvements across all key performance metrics compared to the original logic (Before Optimization). Latency is significantly reduced, request throughput (Req/Sec) and data transfer (Bytes/Sec) are increased, and all timeout errors have been eliminated. This indicates a more efficient and reliable request processing.
1 parent 63428f3 commit 28f111a

File tree

1 file changed

+10
-7
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+10
-7
lines changed

packages/angular/ssr/src/utils/ng.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ export async function renderAngular(
9595
await applicationRef.whenStable();
9696

9797
return {
98-
content: async () => {
99-
try {
100-
return renderInternal(platformRef, applicationRef);
101-
} finally {
102-
await asyncDestroyPlatform(platformRef);
103-
}
104-
},
98+
content: () =>
99+
new Promise<string>((resolve, reject) => {
100+
// Defer rendering to the next event loop iteration to avoid blocking, as most operations in `renderInternal` are synchronous.
101+
setTimeout(() => {
102+
renderInternal(platformRef, applicationRef)
103+
.then(resolve)
104+
.catch(reject)
105+
.finally(() => void asyncDestroyPlatform(platformRef));
106+
}, 0);
107+
}),
105108
};
106109
} catch (error) {
107110
await asyncDestroyPlatform(platformRef);

0 commit comments

Comments
 (0)