-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Different String.startsWith behaviour on Android vs iOS #11370
Comments
Issue persists in RN version 42, and I get it even with usual characters :( More of annoyance than anything, as String.indexOf('subString') == 0 as suffices as a workaround. |
Still exists in RN 44 |
This issue exists because iOS uses many different versions of JavaScriptCore (a different version of iOS 8, 9, 10, 11) and RN Android uses its own JSC version. So there will always be some differences between different platforms until JSC completely stabilizes (maybe never, since JS changes each year). To work around this you could write a polyfill or your own startsWith function instead. |
@ide |
As you can see, the polyfill is only installed if the native version does not exist. In this case, either the JSC implementation on Android should be fixed or the polyfill should be uninstalled unconditionally. It's disconcerting to encounter bugs in the JS standard library. |
In my case, the following expression returns false: If I explicitly set String.prototype.startsWith to the version defined in the polyfill source-- https://github.com/facebook/react-native/blob/master/Libraries/polyfills/String.prototype.es6.js#L20 -- the expression returns true when expected. (The polyfill also matches the source I can view from my browser when attached to the debugger.) (I'd like to see the source used for startsWith when not attached to a debugger, but calling toString() against the function just tells me that it's picking up native code--same result as when the debugger is attached. Is there another technique?) Thanks MatthewAwesome for the (successful) workaround tip. |
The debugger has different behavior because it runs on a different computer and on a different VM. Currently I really recommend that you use it only to debug specific issues rather than the way you develop. This startsWith issue may get fixed in a few months if we are able to upgrade JSC. There is no real ETA though and if this issue is pressing for you I recommend you use your own String polyfill. |
Thanks for the quick response ide! I get the bit about the debugger, thanks. The curiosity I have: (When I was not attached to the debugger,) I substituted the RN source as above (supposedly the polyfill used) and things worked just fine. So without this substitution, when startsWith does not behave correctly, some other source must be getting used--is there an easy way to know where that's coming from? |
You could print |
Unfortunately it just tells me it's [native code]. In node_modules it looks like there are at least a few definitions of String.prototype.startsWith. |
That means it's using the native startsWith implementation then. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
This issue has not been fixed. In this case, two months of inactivity just means that there's nothing more to describe about the bug. Please keep it open. |
I'd forgotten all about this issue but it actually still impacts my app. Can add a temporary work around to my code, but it looks like there are 2 longer term solutions for RN:
Is there any reason not to do 2. as this seems to be a legitimate bug potentially affecting any app that uses As another temporary workaround it might be worth trying this newer version of JSC for Android: https://github.com/SoftwareMansion/jsc-android-buildscripts#how-to-use-it-with-my-react-native-app |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
Please keep it open.
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
Please keep this open 👍 |
Thanks for posting this! It looks like you may not be using the latest version of React Native, v0.53.0, released on January 2018. Can you make sure this issue can still be reproduced in the latest version? I am going to close this, but please feel free to open a new issue if you are able to confirm that this is still a problem in v0.53.0 or newer. |
Cannot reproduce this in v0.53.0 using the Android simulator, tested on:
|
Using v0.55.4 and problem still exist in Android version. |
Thank you @ziyafenn. Can you verify if the issue also exists if you use the JavaScriptCode from https://github.com/react-community/jsc-android-buildscripts? |
I tried "this://44" thing mentioned above but couldn't get incorrect results with that. I could only see incorrect values when emojis are included. Also Rather than checking jsc-android-buildscripts that is not production-ready yet, I'd be more curious to know if this issue happens on iOS 8/9 devices or not, but I don't have any devices to test. If it does not happen this bug might also mean FB's android-jsc is truly incompatible with standard iOS JSC runtimes. |
Hi @patrickkempff. Sorry for a VERY late answer. I checked it with |
Same issue with latest RN |
Still facing this problem. |
@yernandus An alternative (till they fix the issue) that works on both is:
|
@ahmetabdi, thank you. I used lodash startsWith, since lodash was used for other things already. |
RN made the JSC upgrade in master branch, expected release is 0.59 #19737 (comment) This should likely be fixed with that. |
Description
I'm using the
startsWith
method on JavaScript strings to compare some values. Specifically the string contains emoji characters, but not at the beginning of the string.On iOS:
The exact same code on Android:
This may not be an issue of React Native but the underlying JS implementation - just a guess.
Reproduction
Here's a reproduction of the issue: https://rnplay.org/apps/yKn2qA
If you delete the emoji characters from the string the bug goes away.
Additional Information
The text was updated successfully, but these errors were encountered: