From d67c683ba73e12f2819181e4c4986d109341febd Mon Sep 17 00:00:00 2001 From: webstrand Date: Mon, 31 Jul 2023 23:26:56 -0400 Subject: [PATCH] perf: avoid lowercasing long strings --- src/index.ts | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/index.ts b/src/index.ts index aede328..9110722 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,28 +40,30 @@ export function destr(value: any, options: Options = {}): T { return _value.slice(1, -1) as T; } - const _lval = _value.toLowerCase(); - if (_lval === "true") { - return true as T; - } - if (_lval === "false") { - return false as T; - } - if (_lval === "undefined") { - return undefined as T; - } - if (_lval === "null") { - // eslint-disable-next-line unicorn/no-null - return null as T; - } - if (_lval === "nan") { - return Number.NaN as T; - } - if (_lval === "infinity") { - return Number.POSITIVE_INFINITY as T; - } - if (_lval === "-infinity") { - return Number.NEGATIVE_INFINITY as T; + if (_value.length <= 9) { + const _lval = _value.toLowerCase(); + if (_lval === "true") { + return true as T; + } + if (_lval === "false") { + return false as T; + } + if (_lval === "undefined") { + return undefined as T; + } + if (_lval === "null") { + // eslint-disable-next-line unicorn/no-null + return null as T; + } + if (_lval === "nan") { + return Number.NaN as T; + } + if (_lval === "infinity") { + return Number.POSITIVE_INFINITY as T; + } + if (_lval === "-infinity") { + return Number.NEGATIVE_INFINITY as T; + } } if (!JsonSigRx.test(value)) {