Skip to content

Deprecated: Improve $.trim performance for strings with lots of whitespace #461

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

Merged
merged 1 commit into from
Jul 20, 2022

Conversation

vlsi
Copy link
Contributor

@vlsi vlsi commented Apr 25, 2022

The old implementation took O(N^2) time to trim the string when multiple adjacent spaces
were present.

For instance, consider the string "A B" (10 spaces between A and B).
Then old regexp /[\s]+$/ would take 10 steps until it realizes the regexp does not match at position 1.
Then it would try to match the regexp at position 2, and it would take 9 steps.
Then 8 steps for position 3, ... so it would take 10*10/2 steps until it figures out the regexp does not match.

The new approach is to require "non-whitespace" char before the whitespace run, so it spends just one step for each space in the string.

Note: if String.prototype.trim() is good enough, then the implementation would use it.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Apr 25, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: vlsi / name: Vladimir Sitnikov (c962c28)

@vlsi vlsi force-pushed the faster_trim branch 2 times, most recently from 001ac34 to 185d4b5 Compare April 25, 2022 12:56
@mgol
Copy link
Member

mgol commented Jun 27, 2022

Thanks for the PR.

With Migrate, we're more concerned about backwards compatibility than performance. This is why implementation of various methods is based on how they were implemented in jQuery before deprecation/removal.

The implementation here shouldn't be changed before the upstream version in jQuery is updated; you can find it at https://github.com/jquery/jquery/blob/3.x-stable/src/deprecated.js. One advantage of making a change in jQuery first is that it has a more comprehensive test suite so any mistakes in the implementation would be detected better.

That said, since the util is deprecated, we'd rather not increase the library size to address an edge case that people shouldn't rely on anyway as we recommend migrating to native trim. Please take that into account.

Since any action should first be taken in jQuery upstream, I'm going to close the PR. If a change to jQuery gets accepted, we can then backport the change here.

(note: when submitting a PR, please address any checks errors GitHub throws at you; for example, this PR had a lot of code style errors)

@mgol mgol closed this Jun 27, 2022
@vlsi
Copy link
Contributor Author

vlsi commented Jun 27, 2022

@mgol , the exact .trim method has been removed from the base jQuery, so I believe there's no way the contribution could be accepted to jQuery: jquery/jquery@0b676ae


That said, since the util is deprecated, we'd rather not increase the library size to address an edge case that people shouldn't rely on anyway as we recommend migrating to native trim. Please take that into account.

My edge case is that my JIRA instance takes 20 seconds to display an issue (it spends most of that time within jquery.trim).

The implementation is really straightforward, and it doesn't make jquery-migrate significantly bigger.

note: when submitting a PR, please address any checks errors GitHub throws at you; for example, this PR had a lot of code style errors

I am OK to fix the issues if you merge the PR, however,

  1. The PR has been "in review" for 2 months
  2. You seem to want to close the PR even though it impacts the end-users. Do you really suggest that I should spend MY time on fixing the CI issues with the only outcome of you closing the PR as "won't merge"?

@mgol
Copy link
Member

mgol commented Jun 27, 2022

It was removed from the main branch of jQuery; this is the branch where we're merging changes that will land in jQuery 4.0.0 when it's ready. The latest stable line is 3.x and it's developed on the 3.x-stable branch; the PR would have to be submitted against this branch.

I am OK to fix the issues if you merge the PR, however,

1. The PR has been "in review" for 2 months

This project is volunteer-driven, I don't have active support contract with anyone. And it so happened that I was on vacation recently and before the vacation I was busy at work with hardly any time for open source work.

I'm reviewing PRs when I have time. Usually it doesn't take two months but from time to time I have less time for OSS; you can't expect me to always reply within a given time frame.

2. You seem to want to close the PR even though it impacts the end-users. Do you really suggest that I should spend MY time on fixing the CI issues with the only outcome of you closing the PR as "won't merge"?

I explained why changes need to land in jQuery upstream first, at least as long as the code is still maintained there which is the case here. My time is limited. I'd be happy to review your PR to jQuery.

When you submit a PR to an OSS project, you can't always expect it will get merged; sometimes it doesn't align with the project priorities. In case of jQuery, if you want to avoid spending time on CI issues only to see your PR rejected, it's a good idea to open an issue first, present a concrete use case (like the JIRA example you mentioned here - without a concrete case like that, we can't know if the issue is worth fixing) and wait for the team to approve the approach; then your PR may still get requests for changes but will generally be merged when those are addressed.

For the record, I think this would be a good change to land, although we'd like to compress the code a bit. It's also likely that we'd prefer to use the regex approach in all browsers to make the code smaller; that should still resolve your performance concerns as much as I understand.

@mgol
Copy link
Member

mgol commented Jul 20, 2022

@vlsi Since jquery/jquery#5068 landed, I'm going to reopen this PR. We can merge it if you update the implementation here to match the one that landed in the jQuery PR.

@mgol mgol reopened this Jul 20, 2022
Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking as "Request changes" until the implementation is updated to match the jQuery one from jquery/jquery#5068.

…s of whitespaces

The old implementation took O(N^2) time to trim the string when multiple adjacent spaces
were present.

For instance, consider the string "A          B" (10 spaces between A and B).
Then old regexp /[\s]+$/ would take 10 steps until it realizes
the regexp does not match at position 1.
Then it would try to match the regexp at position 2, and it would take 9 steps.
Then 8 steps for position 3, ... so it would take 10*10/2 steps until
it figures out the regexp does not match.

See jquery/jquery#5068

The new approach is to require "non-whitespace" char before the whitespace run,
so it spends just one step for each space in the string.
Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mgol mgol added this to the 3.4.1 milestone Jul 20, 2022
@mgol mgol changed the title Performance: improve .trim performance for large strings contains lots of whitespaces Deprecated: Improve $.trim performance for strings with lots of whitespace Jul 20, 2022
@mgol mgol merged commit 69a2441 into jquery:main Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants