admin管理员组文章数量:1333612
word.includes(str)
returns a boolean
indicating whether or not str
is included in word
word.search(str)
returns a number
representing the position of the first str
found in word
, -1
if it does not exist.
At first, I wondered why JavaScript provides both, however it's possible to create only one method that works for both cases, returns position else returns undefined
, but since both exist, so it should be for a reason, maybe String.includes
is faster, or maybe, it's just to make things clearer.
It looks like String.includes
should be faster as it only checks if str
exists rather than trying to find its position, however, my simple thought: to check if str
exists in word
the algorithm should loop through the word
characters anyway (if there is something I'm missing let me know) and once str
is found, it can easily retrieve its position, so String.includes
couldn't be faster.
I want to know the real difference between them and why both exist. thank you!
word.includes(str)
returns a boolean
indicating whether or not str
is included in word
word.search(str)
returns a number
representing the position of the first str
found in word
, -1
if it does not exist.
At first, I wondered why JavaScript provides both, however it's possible to create only one method that works for both cases, returns position else returns undefined
, but since both exist, so it should be for a reason, maybe String.includes
is faster, or maybe, it's just to make things clearer.
It looks like String.includes
should be faster as it only checks if str
exists rather than trying to find its position, however, my simple thought: to check if str
exists in word
the algorithm should loop through the word
characters anyway (if there is something I'm missing let me know) and once str
is found, it can easily retrieve its position, so String.includes
couldn't be faster.
I want to know the real difference between them and why both exist. thank you!
Share Improve this question asked Mar 18, 2023 at 2:13 Ahmed SbaiAhmed Sbai 16.3k11 gold badges30 silver badges52 bronze badges 7-
1
Does this answer your question? It includes a parison of numerous methods to search a string. (
string.search
is also intended to be used with regular expressions) – grimsteel Commented Mar 18, 2023 at 2:21 - it does not sorry – Ahmed Sbai Commented Mar 18, 2023 at 2:24
-
"[I]t's possible to create only one method that works for both cases, returns position else returns undefined" No; a function returns undefined if a value was not returned (MDN Web Docs) and therefore using
undefined
as a return value is unconventional. – InSync Commented Mar 18, 2023 at 2:24 -
"it's possible to create only one method that works for both cases" - sure that's possible, but it would be awkward to use. You'd have to write
if (word.something(str) != undefined)
all the time. Just as weird as, but even longer than, the existingif (word.indexOf(str) > -1)
orif (word.search(regex) > -1)
. That's whyif (word.includes(str))
was created - for convenience and clarity. It has more to do with the speed of the developer writing, reading and understanding the code, than with the speed of the string search algorithm. – Bergi Commented Mar 18, 2023 at 2:35 -
1
@AhmedSbai No you don't write
array.find(…)
, unless you actually want to get the value back (andundefined
if not found). If you just want to know whether the value exists, you usearray.some(…)
which appropriately returns a boolean. – Bergi Commented Mar 18, 2023 at 3:03
2 Answers
Reset to default 7The big difference between the two is that search()
takes a regex - much more flexible and (probably) slower than the match in includes()
that can be done quickly with something like boyer-moore
The panion to includes()
is indexOf()
which is probably very parable algorithmically. With that pair, it could make sense to build includes() as a bool-returning wrapper around indexOf().
String.prototype.includes()
is basically a convenience method which just uses String.prototype.indexOf()
. They both accept a string argument and an optional (start) position argument. They try to find a matching substring value inside the string.
String.prototype.search()
is then better pared to String.prototype.indexOf()
because they both return the index of the matched content.
.search()
works differently because it performs a regex search, while .indexOf()
just tries to find the exact substring.
Thus it's easy to conclude that .search()
must always be slower than .indexOf()
, and therefore slower than .contains()
, because checking if a substring is equal to a value is slower than checking if a substring matches a regex rule.
本文标签: javascriptis Stringincludes faster than StringsearchStack Overflow
版权声明:本文标题:javascript - is String.includes faster than String.search? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742280257a2445962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论