From 0eaf189431b271928b0a3152317710eb046f1b1f Mon Sep 17 00:00:00 2001 From: Michael Bullington Date: Tue, 9 Jun 2020 00:07:19 -0400 Subject: [PATCH 1/2] optimize parseLiteral for number-heavy JSON files (ala GeoJSON) --- src/impl/parser.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/impl/parser.ts b/src/impl/parser.ts index 462d494..aab0b8b 100644 --- a/src/impl/parser.ts +++ b/src/impl/parser.ts @@ -482,16 +482,21 @@ export function visit(text: string, visitor: JSONVisitor, options: ParseOptions function parseLiteral(): boolean { switch (_scanner.getToken()) { case SyntaxKind.NumericLiteral: - let value = 0; - try { - value = JSON.parse(_scanner.getTokenValue()); - if (typeof value !== 'number') { + const tokenValue = _scanner.getTokenValue(); + let value = parseFloat(tokenValue); + + if (isNaN(value)) { + try { + value = JSON.parse(tokenValue); + if (typeof value !== 'number') { + handleError(ParseErrorCode.InvalidNumberFormat); + value = 0; + } + } catch (e) { handleError(ParseErrorCode.InvalidNumberFormat); - value = 0; } - } catch (e) { - handleError(ParseErrorCode.InvalidNumberFormat); } + onLiteralValue(value); break; case SyntaxKind.NullKeyword: From 7505449c94ce59dc1db327949b71d1e7d7dcd91b Mon Sep 17 00:00:00 2001 From: Michael Bullington Date: Tue, 9 Jun 2020 23:29:18 -0400 Subject: [PATCH 2/2] switch from parseFloat to Number --- src/impl/parser.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/impl/parser.ts b/src/impl/parser.ts index aab0b8b..fcae231 100644 --- a/src/impl/parser.ts +++ b/src/impl/parser.ts @@ -483,18 +483,11 @@ export function visit(text: string, visitor: JSONVisitor, options: ParseOptions switch (_scanner.getToken()) { case SyntaxKind.NumericLiteral: const tokenValue = _scanner.getTokenValue(); - let value = parseFloat(tokenValue); + let value = Number(tokenValue); if (isNaN(value)) { - try { - value = JSON.parse(tokenValue); - if (typeof value !== 'number') { - handleError(ParseErrorCode.InvalidNumberFormat); - value = 0; - } - } catch (e) { - handleError(ParseErrorCode.InvalidNumberFormat); - } + handleError(ParseErrorCode.InvalidNumberFormat); + value = 0; } onLiteralValue(value);