From f15a845b76b5441b261c3adf758035854082d0ec Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 7 Jul 2021 01:58:06 -0500 Subject: [PATCH] perf: merge the if conditions --- Readme.md | 2 +- src/native/lib.d | 10 +++++----- test/fixtures/escaped_quote.json | 6 +++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 130e349..660f28e 100644 --- a/Readme.md +++ b/Readme.md @@ -70,7 +70,7 @@ minifyFiles(["file1.json", "file2.json"]); ``` ❯ node .\benchmark\native-benchmark.mjs -1.086 seconds +1.066 seconds ❯ node .\benchmark\js-benchmark.mjs 58.686 seconds diff --git a/src/native/lib.d b/src/native/lib.d index 3c9a324..89a611f 100644 --- a/src/native/lib.d +++ b/src/native/lib.d @@ -42,9 +42,13 @@ string minifyString(string jsonString, bool hasComments = false) @trusted const lastIndex = jsonString.length - rightContext.length; + auto leftContextSubstr = leftContext[from .. $]; + + // update from for the next iteration + from = lastIndex; + const noCommentOrNotInComment = !hasComments || (!in_multiline_comment && !in_singleline_comment); - auto leftContextSubstr = leftContext[from .. $]; if (noCommentOrNotInComment) { if (!in_string) @@ -52,11 +56,7 @@ string minifyString(string jsonString, bool hasComments = false) @trusted leftContextSubstr = leftContextSubstr.replaceAll(spaceOrBreakRegex, ""); } new_str ~= leftContextSubstr; - } - from = lastIndex; - if (noCommentOrNotInComment) - { if (matchFrontHit == "\"") { if (!in_string || hasNoSlashOrEvenNumberOfSlashes(leftContextSubstr)) diff --git a/test/fixtures/escaped_quote.json b/test/fixtures/escaped_quote.json index 5818377..e4b5440 100644 --- a/test/fixtures/escaped_quote.json +++ b/test/fixtures/escaped_quote.json @@ -1,3 +1,7 @@ { - "a": "\" a " + "a": "\" a ", + "b": "\\\" a ", + "c": "\\\\\" a ", + "d": "a\\\\\\", + "e": "\\\\\\" }