From 20f5dc5103ec3d7fe2c4e2d898ee689696e8a0e8 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 29 Jan 2025 08:23:20 +0200 Subject: [PATCH] refactor(core): simplify `concatStringsWithSpace` (#59820) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new version of the function is smaller, eliminating extra bytes. The refactor improves both code size and readability while optimizing the implementation. Benchmark results for the old and new implementations are as follows: ``` concatStringsWithSpace_old x 149,225,311 ops/sec ±8.54% (50 runs sampled) concatStringsWithSpace_new x 160,206,834 ops/sec ±5.72% (54 runs sampled) ``` Thus, the new implementation is both smaller and faster. PR Close #59820 --- packages/core/src/util/stringify.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/core/src/util/stringify.ts b/packages/core/src/util/stringify.ts index 6545d9ee566..916ddeaeb49 100644 --- a/packages/core/src/util/stringify.ts +++ b/packages/core/src/util/stringify.ts @@ -46,13 +46,9 @@ export function stringify(token: any): string { * @returns concatenated string. */ export function concatStringsWithSpace(before: string | null, after: string | null): string { - return before == null || before === '' - ? after === null - ? '' - : after - : after == null || after === '' - ? before - : before + ' ' + after; + if (!before) return after || ''; + if (!after) return before; + return `${before} ${after}`; } /**