-
Notifications
You must be signed in to change notification settings - Fork 382
Use JS String includes/startsWith/endsWith #10170
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
Conversation
|
Let's wait and merge after #10106, just in case there's a merge issue so it goes in this PR. |
|
Maybe you like to replace String.replace (which is petty heavyweight) with JS String.replaceAll too? |
Can you elaborate? GWT's String.replace already calls String.replaceAll, with an extra On the other hand, Java String.replaceAll takes a string as regex (as both JS String.replace and String.replaceAll do), but String.replace takes a char or string, and doesn't treat the first arg as a regex. The heavyweight parts of GWT's String.replace is making the passed string literal into a regexp (plus then calling GWT's own String.replaceAll adds backref replacements). I agree there is probably some savings to be had here, but it doesn't look substantial, and there is no direct analog like there is for these three methods? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll |
|
I'm pretty confident that beats with additional cost of in complexity, size and performance (not measured). |
|
Thanks, it looks like I misread the definition, as I'll take a quick look, if its that easy, its definitely worth it. |
|
Simplifying |
|
replace(char, char) does get slightly messy (inlining with constant chars doesn't often end happily today, I have a followup to help with that), but definitely worth a look too, yes. |
|
@zbynek aside: it looks like adding any label to a PR triggers a build now if |
|
I'm using this personally, while also transforming all constant chars to strings by a preprocessor (until your cleaner solution is available). I think that should inline quite ok. |
|
Consider String.valueOf rather than directly using NativeString.fromCharCode - the followup I'm working on deals with the fact that if you have |
|
Digging in more deeply, the proposed replacement is not enough, replaceAll(String, String) in JS doesn't quite behave the same as replace(String, String), though it looks like the old Example in Java (caught by GWT's unit tests): Whereas in Javascript (tested chrome, ff, safari): Failing unit test: Additionally, some further testing suggests that Chrome doesn't behave the way it used to when it comes to escaped slashes, and our unit tests fail when run in a browser - so even what we have today is wrong, and needs more vetting. ...So since we need at least part of the old function and the plain JS call isn't enough, I'm going to leave out replace/replaceAll from this patch for them to be addressed later, and in a more concrete way. Check your own impl @vjay82 - maybe you never use a replacement |
|
For the time being I will probably go with this as a quick fix: Using a function is likely not cheap but then relying on a regex and/or lots of transformations is probably neither. |
Even when completely inlined, should produce smaller code than the previous emulation, and depending on the browser implementation this could perform slightly better as well.