improve performance, alternative approach (+149% throughput and more)#38
Merged
sindresorhus merged 2 commits intochalk:mainfrom Mar 24, 2023
Merged
improve performance, alternative approach (+149% throughput and more)#38sindresorhus merged 2 commits intochalk:mainfrom
sindresorhus merged 2 commits intochalk:mainfrom
Conversation
AlCalzone
commented
Mar 20, 2023
|
|
||
| test('support true color escape sequences', t => { | ||
| t.is(sliceAnsi('\u001B[1m\u001B[48;2;255;255;255m\u001B[38;2;255;0;0municorn\u001B[39m\u001B[49m\u001B[22m', 0, 3), '\u001B[1m\u001B[48;2;255;255;255m\u001B[38;2;255;0;0muni\u001B[22m\u001B[49m\u001B[39m'); | ||
| t.is(sliceAnsi('\u001B[1m\u001B[48;2;255;255;255m\u001B[38;2;255;0;0municorn\u001B[39m\u001B[49m\u001B[22m', 0, 3), '\u001B[1m\u001B[48;2;255;255;255m\u001B[38;2;255;0;0muni\u001B[39m\u001B[49m\u001B[22m'); |
Contributor
Author
There was a problem hiding this comment.
after this change, the start codes get undone in the same order as in the input string.
| t.is(sliceAnsi(output, 0, 7), `${chalk.black.bgYellow(' RUNS ')} `); | ||
| t.is(sliceAnsi(output, 0, 8), `${chalk.black.bgYellow(' RUNS ')} `); | ||
| t.is(JSON.stringify(sliceAnsi('\u001B[31m' + output, 0, 4)), JSON.stringify(`\u001B[31m${chalk.black.bgYellow(' RUN')}`)); | ||
| t.is(JSON.stringify(sliceAnsi('\u001B[31m' + output, 0, 4)), JSON.stringify(chalk.black.bgYellow(' RUN'))); |
Contributor
Author
There was a problem hiding this comment.
The ANSI code for yellow is unnecessary in the output, because it immediately gets overwritten with black
Member
|
I like this approach. Tokenizing it first makes a lot of sense. |
sindresorhus
requested changes
Mar 24, 2023
Member
|
Thanks :) |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR is an alternative to #37
The idea is to first analyze the input string and turn it into an array of ANSI codes and characters. Slicing is then done by operating on the array. It basically rewrites the entire library, but yields a higher performance (+149% throughput, and more when reusing some of the work for slicing the same string multiple times). For my methodology on testing the performance, see #37. Some of the improvements are also taken from there.
I had to change two tests though - one unnecessarily used two separate foreground colors where the 2nd overwrote the 1st. And in another one, the expected string had the end codes in the same order as the start codes, while all others had it in the opposite order. I didn't see any difference when printing those strings.
and if the result of the tokenization is memorized (export
tokenizefunction, and add a copy of thesliceAnsifunction which operates on a token array, instead of a string) , repeated slices on the same input (like in the test case) are much faster (+300% throughput):FYI, I have TypeScript code for this change. Let me know if that is preferred.
closes: #37