From 55cec8b522f4eac1636e03511c3683f7f04f9f47 Mon Sep 17 00:00:00 2001 From: Ame_x Edam Date: Thu, 28 Nov 2024 03:21:34 +0000 Subject: [PATCH] perf&refactor: use `!!v` insetead of `Boolean(v)` to reduce bundle size and improve performance --- src/helper/streaming/sse.ts | 2 +- src/jsx/streaming.ts | 22 +++++++++---------- src/middleware/combine/index.ts | 2 +- .../secure-headers/secure-headers.ts | 2 +- src/utils/html.ts | 7 +++--- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/helper/streaming/sse.ts b/src/helper/streaming/sse.ts index bdd6d00fd..f19481b06 100644 --- a/src/helper/streaming/sse.ts +++ b/src/helper/streaming/sse.ts @@ -30,7 +30,7 @@ export class SSEStreamingApi extends StreamingApi { message.id && `id: ${message.id}`, message.retry && `retry: ${message.retry}`, ] - .filter(Boolean) + .filter((data) => !!data) .join('\n') + '\n\n' await this.write(sseData) diff --git a/src/jsx/streaming.ts b/src/jsx/streaming.ts index cbdb4fd55..9a9bb3cd6 100644 --- a/src/jsx/streaming.ts +++ b/src/jsx/streaming.ts @@ -129,7 +129,7 @@ export const renderToReadableStream = ( async start(controller) { try { if (content instanceof JSXNode) { - // aJSXNode.toString() returns a string or Promise and string is already escaped + // JSXNode.toString() returns a string or Promise and string is already escaped content = content.toString() as HtmlEscapedString | Promise } const context = typeof content === 'object' ? content : {} @@ -158,21 +158,21 @@ export const renderToReadableStream = ( true, context ) - ;(res as HtmlEscapedString).callbacks - ?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context })) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - .filter>(Boolean as any) - .forEach(then) + ;( + (res as HtmlEscapedString).callbacks + ?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context })) + .filter((result) => !!result) as Promise[] + ).forEach(then) resolvedCount++ controller.enqueue(textEncoder.encode(res)) }) ) } - ;(resolved as HtmlEscapedString).callbacks - ?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context })) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - .filter>(Boolean as any) - .forEach(then) + ;( + (resolved as HtmlEscapedString).callbacks + ?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context })) + .filter((result) => !!result) as Promise[] + ).forEach(then) while (resolvedCount !== callbacks.length) { await Promise.all(callbacks) } diff --git a/src/middleware/combine/index.ts b/src/middleware/combine/index.ts index 2a258bc35..f0f69a668 100644 --- a/src/middleware/combine/index.ts +++ b/src/middleware/combine/index.ts @@ -144,7 +144,7 @@ export const except = ( return condition } }) - .filter(Boolean) as Condition[] + .filter((condition) => !!condition) as Condition[] if (router) { conditions.unshift((c: Context) => !!router?.match(METHOD_NAME_ALL, c.req.path)?.[0]?.[0]?.[0]) diff --git a/src/middleware/secure-headers/secure-headers.ts b/src/middleware/secure-headers/secure-headers.ts index 55d7aa970..5300a8478 100644 --- a/src/middleware/secure-headers/secure-headers.ts +++ b/src/middleware/secure-headers/secure-headers.ts @@ -306,7 +306,7 @@ function getPermissionsPolicyDirectives(policy: PermissionsPolicyOptions): strin return '' }) - .filter(Boolean) + .filter((directive) => !!directive) .join(', ') } diff --git a/src/utils/html.ts b/src/utils/html.ts index 855fa160f..e64a8eebc 100644 --- a/src/utils/html.ts +++ b/src/utils/html.ts @@ -167,10 +167,9 @@ export const resolveCallback = async ( const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then((res) => Promise.all( - res - // eslint-disable-next-line @typescript-eslint/no-explicit-any - .filter(Boolean as any) - .map((str) => resolveCallback(str, phase, false, context, buffer)) + (res.filter((res) => !!res) as string[]).map((str) => + resolveCallback(str, phase, false, context, buffer) + ) ).then(() => (buffer as [string])[0]) )