From 0da26c4b5d97a2082a798b247f19f7c098a45ab4 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Wed, 15 Jan 2025 11:52:03 +0900 Subject: [PATCH] perf: try to improve the performance of `transformQueryLog` --- packages/backend/src/postgres.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/postgres.ts b/packages/backend/src/postgres.ts index d09240eba1c9..1004d61ffc6b 100644 --- a/packages/backend/src/postgres.ts +++ b/packages/backend/src/postgres.ts @@ -100,8 +100,8 @@ function highlightSql(sql: string) { }); } -function truncateSql(sql: string) { - return sql.length > 100 ? `${sql.substring(0, 100)}...` : sql; +function truncateSql(sql: string): [string, boolean] { + return sql.length > 100 ? [`${sql.substring(0, 100)}`, true] : [sql, false]; } function stringifyParameter(param: any) { @@ -117,13 +117,16 @@ class MyCustomLogger implements Logger { } @bindThis - private transformQueryLog(sql: string) { + private transformQueryLog(sql: string): string { let modded = sql; + let truncated: boolean = false; if (!this.props.disableQueryTruncation) { - modded = truncateSql(modded); + [modded, truncated] = truncateSql(modded); } - return highlightSql(modded); + modded = highlightSql(modded); + if (truncated) modded += '...'; + return modded; } @bindThis