4/5/2023 0 Comments Javascript includes arrayIf you don’t care, just call includes because it makes the intent of your code more clear. You should use indexOf if you care about where the substring is in the original string. So while includes will be a tiny, tiny amount slower because it has to check if you passed it a regex, in reality this will make no difference to how fast your code runs. indexOf will accept a regular expression but always return -1, which isn’t too helpful. The only difference is that includes checks if you’ve passed it a regular expression instead of a string, and throws an exception if you have. In the case of indexOf vs includes, you’ll find them right next to each other here in the ECMAScript spec. ![]() Let’s take a look at the ECMAScript specification to see what it says. ![]() The user who asked it was curious whether there would be any performance difference between using a JavaScript string’s indexOf vs includes performance when trying to find a substring within a larger string. This is a short response I wrote to a question on /r/javascript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |