From b7a909e64459b606996e5cc2e77c1c5568c1736b Mon Sep 17 00:00:00 2001 From: PondWader <66561610+PondWader@users.noreply.github.com> Date: Sun, 23 Jun 2024 19:29:25 +0100 Subject: [PATCH] perf: don't read existing values when parsing without array repeat (#37) --- src/parse.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/parse.ts b/src/parse.ts index cdb0834..ef8547e 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -138,18 +138,18 @@ export function parse(input: string, options?: ParseOptions): ParsedQuery { } const newValue = valueDeserializer(value, currentKey); - const currentValue = currentObj[currentKey]; - - if (currentValue === undefined || !arrayRepeat) { - currentObj[currentKey] = newValue; - } else if (arrayRepeat) { + if (arrayRepeat) { + const currentValue = currentObj[currentKey]; + if (currentValue === undefined) { + currentObj[currentKey] = newValue; + } // Optimization: value.pop is faster than Array.isArray(value) - if ((currentValue as unknown[]).pop) { + else if ((currentValue as unknown[]).pop) { (currentValue as unknown[]).push(newValue); } else { currentObj[currentKey] = [currentValue, newValue]; } - } + } else currentObj[currentKey] = newValue; } // Reset reading key value pairs