Skip to content

Commit

Permalink
perf: merge the if conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jul 7, 2021
1 parent 6f33d71 commit f15a845
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/native/lib.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ 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)
{
leftContextSubstr = leftContextSubstr.replaceAll(spaceOrBreakRegex, "");
}
new_str ~= leftContextSubstr;
}
from = lastIndex;

if (noCommentOrNotInComment)
{
if (matchFrontHit == "\"")
{
if (!in_string || hasNoSlashOrEvenNumberOfSlashes(leftContextSubstr))
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/escaped_quote.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"a": "\" a "
"a": "\" a ",
"b": "\\\" a ",
"c": "\\\\\" a ",
"d": "a\\\\\\",
"e": "\\\\\\"
}

0 comments on commit f15a845

Please sign in to comment.