Note that cached RegExp is: var r = new RegExp('simple') var c = r.test(str) as opposed to: /simple/. Just to save people from running the tests themselves, here are the current results for most common browsers, the percentages indicate performance increase over the next fastest result (which varies between browsers): For example a case-insensitive search is way faster with regular expressions. ![]() While in Chrome 10 indexOf seems to be faster, in Safari 5, indexOf is clearly slower than any other method. The differences between the browsers is enormous. In javascript the includes () method can be used to determines whether a string contains particular word (or characters at specified position). If you have user-generated content in your web application, chances are you have to deal with strings containing emojis. It cannot be said with certainty which method is faster. Regular expressions seem to be faster (at least in Chrome 10). So you can use JavaScript IndexOf () method to check if a string contains substring in it. ![]() Regular expression: (new RegExp('word')).test(str) The JavaScript indexOf () method search for the substring in string and returns the position of the first occurrence of a specified substring.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |