perf(format): cache breaklines and spaces as much as possible#81
perf(format): cache breaklines and spaces as much as possible#81aeschli merged 1 commit intomicrosoft:mainfrom
Conversation
|
@microsoft-github-policy-service agree |
| let indentValue: string; | ||
| if (options.insertSpaces) { | ||
| indentValue = repeat(' ', options.tabSize || 4); | ||
| indentValue = cachedSpaces[options.tabSize || 4] ?? repeat(cachedSpaces[1], options.tabSize || 4); |
There was a problem hiding this comment.
the ?? operator is a breaking change for node 12. since there's no engines declaration, everything that worked in 3.0.0 is part of the public API.
There was a problem hiding this comment.
The minimum version of this package is 14.x, so I think this is expected, it worked before because of luck not because was the intention.
An PR can be created to fix this but probably this can happen more times in the future.
There was a problem hiding this comment.
That's only the case if it's declared in engines.node - intention doesn't matter for semver.
The way to avoid it happening in the future, after fixing it and releasing a patch, is to either add node 12 into the CI matrix, or, to immediately add engines.node and do a major bump.
There was a problem hiding this comment.
Yes, the minimum is now 16.x.
There was a problem hiding this comment.
Then this should have been a v4.0.0 that included an explicit engines.node declaration.
This PR basically tries to cache as much as possible the strings and spaces created since they are basically the same, there's no need to allocate new memory for those strings.
This optimization affects
eol:\r\n\r\nThis optimization will only cache strings up to
200characters (it was chosen just because it speedupstabSize=4for this huge json).In the happy path, this algorithm will only allocate memory for the array and the objects, but not for the
content.For smaller files, this change didn't make the code run faster, this is only valid for larger json.
Before:
After:
Benchmark:
Large Json: rrdom-benchmark-1.json.zip