-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export const cachedSpaces = new Array(20).fill(0).map((_, index) => { | ||
return ' '.repeat(index); | ||
}); | ||
|
||
const maxCachedValues = 200; | ||
|
||
export const cachedBreakLinesWithSpaces = { | ||
' ': { | ||
'\n': new Array(maxCachedValues).fill(0).map((_, index) => { | ||
return '\n' + ' '.repeat(index); | ||
}), | ||
'\r': new Array(maxCachedValues).fill(0).map((_, index) => { | ||
return '\r' + ' '.repeat(index); | ||
}), | ||
'\r\n': new Array(maxCachedValues).fill(0).map((_, index) => { | ||
return '\r\n' + ' '.repeat(index); | ||
}), | ||
}, | ||
'\t': { | ||
'\n': new Array(maxCachedValues).fill(0).map((_, index) => { | ||
return '\n' + '\t'.repeat(index); | ||
}), | ||
'\r': new Array(maxCachedValues).fill(0).map((_, index) => { | ||
return '\r' + '\t'.repeat(index); | ||
}), | ||
'\r\n': new Array(maxCachedValues).fill(0).map((_, index) => { | ||
return '\r\n' + '\t'.repeat(index); | ||
}), | ||
} | ||
}; | ||
|
||
export const supportedEols = ['\n', '\r', '\r\n'] as const; | ||
export type SupportedEOL = '\n' | '\r' | '\r\n'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { cachedBreakLinesWithSpaces, cachedSpaces, supportedEols } from '../impl/string-intern'; | ||
import * as assert from 'assert'; | ||
|
||
suite('string intern', () => { | ||
test('should correctly define spaces intern', () => { | ||
for (let i = 0; i < cachedSpaces.length; i++) { | ||
assert.strictEqual(cachedSpaces[i], ' '.repeat(i)); | ||
} | ||
}); | ||
|
||
test('should correctly define break lines with spaces intern', () => { | ||
for (const indentType of [' ', '\t'] as const) { | ||
for (const eol of supportedEols) { | ||
for (let i = 0; i < cachedBreakLinesWithSpaces[indentType][eol].length; i++) { | ||
assert.strictEqual(cachedBreakLinesWithSpaces[indentType][eol][i], eol + indentType.repeat(i)); | ||
} | ||
} | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.