improve performance, alternative approach (+149% throughput and more) #38
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
tokenize
function, and add a copy of thesliceAnsi
function 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