Helo #JavaScript and TypeScript programmers.
Uncategorized
1
Posts
1
Posters
3
Views
-
Helo #JavaScript and TypeScript programmers. (To which I don't count myself, even if I have to use it a bit now and then.)
This is an anti-pattern, right?
if (foo.indexOf("bar") === 0) {
...
}Because if foo does *not* start with "bar", indexOf() will still search through all of foo for "bar". Even if you are only interested in seeing whether foo *starts* with "bar".
Or are JavaScript interpeters and JITters clever enough to recognise this and silently turn it into effectively a use of startsWith() anyway? I suspect so. But still, nicer to write it optimally (and more obviously) from the start, right? Like this:
if (foo.startsWith("bar") {
...
} -
undefined Oblomov shared this topic on