-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(format): cache breaklines and spaces as much as possible #81
Conversation
@microsoft-github-policy-service agree |
|
||
let numberLineBreaks = 0; | ||
|
||
let indentLevel = 0; | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the minimum is now 16.x.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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\n
This optimization will only cache strings up to
200
characters (it was chosen just because it speedupstabSize=4
for 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