From 163823dc3ce8e43aa71684bdb7f027e4011128a9 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Tue, 22 Apr 2025 21:46:27 +0200 Subject: [PATCH] Prevent regexp recreation on function call It will improve memory and CPU cycles usage. Init regexp once and reuse :) --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 94cebc9..91c9ff6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,8 @@ import type { TAnySchema, TRecord } from '@sinclair/typebox' const Kind = Symbol.for('TypeBox.Kind') const OptionalKind = Symbol.for('TypeBox.Optional') - -const isSpecialProperty = (name: string) => /(\ |-|\t|\n)/.test(name) +const SPECIAL_PROP_REGEXP = /(\ |-|\t|\n)/ +const isSpecialProperty = (name: string) => SPECIAL_PROP_REGEXP.test(name) const joinProperty = (v1: string, v2: string | number) => { if (typeof v2 === 'number') return `${v1}[${v2}]`