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
Chore: improve performance of SourceCode constructor #8054
Conversation
When generating a list of tokens and comments, the `SourceCode` constructor previously concatenated the list of tokens and the list of comments, and sorted the result. For a file with `n` tokens and `n` comments, this takes `O(nlog(n))` time. However, since the token and comment lists are already sorted, they can be merged in `O(n)` time. The performance impact of this change is most noticable on large files, since they have a large number of tokens. When linting [test-data.js](https://gist.github.com/mysticatea/2fca6bc8c2194f75e4808a884a1e351a) (48000 lines) with no rules enabled, this improved overall performance by about 20%.
@not-an-aardvark, thanks for your PR! By analyzing the history of the files in this pull request, we identified @nzakas, @mysticatea and @gyandeeps to be potential reviewers. |
LGTM |
Can you post the results of running |
There doesn't seem to be any notable difference with the npm run perf on master
npm run perf on this branch
On the other hand, there is a significant difference when linting a very large file. Linting test-data.js on master
Linting test-data.js on this branch
|
What is the purpose of this pull request? (put an "X" next to item)
[x] Other, please explain:
What changes did you make? (Give an overview)
When generating a list of tokens and comments, the
SourceCode
constructor previously concatenated the list of tokens and the list of comments, and sorted the result. For a file withn
tokens andn
comments, this takesO(nlog(n))
time. However, since the token and comment lists are already sorted, they can be merged inO(n)
time.The performance impact of this change is most noticeable on large files, since they have a large number of tokens. When linting test-data.js (48000 lines) with no rules enabled, this improved overall performance by about 15-20%.
Is there anything you'd like reviewers to focus on?
Nothing in particular