4

Here's Google's implementation of String.startsWith() taken from Closure Library:

goog.string.startsWith = function(str, prefix) {
   return str.lastIndexOf(prefix, 0) == 0;
};

I was wondering why did they choose lastIndexOf over indexOf given the fact that:

  1. indexOf is twice as fast in some cases.
  2. Browser compatibility is the same.

It's not about micro-optimization, I really do believe that:

return str.indexOf(prefix) == 0;

is simpler, cleaner and happened to be faster than:

return str.lastIndexOf(prefix, 0) == 0;

How is lastIndexOf better in this case?

resu
  • 944
  • 12
  • 21
  • `lastIndexOf` _should_ be faster in theory. – tkausl Jul 08 '18 at 22:51
  • [some quick testing](https://jsperf.com/indexof-vs-lastindexof-google) shows that `lastIndexOf()` is in fact 96% slower than `indexOf()` – Luca Kiebel Jul 08 '18 at 22:55
  • @Luca the 800M ops per second means it was optimised out by an engine entirely and was running nothing in a loop. – zerkms Jul 08 '18 at 23:11
  • @SheshankS. what answer there addresses this question? – zerkms Jul 08 '18 at 23:12
  • @zerkms Look at your question title. Your title has nothing to do with google, it only has to do with the difference between indexOf and lastIndexOf – Sheshank S. Jul 08 '18 at 23:13
  • 2
    @SheshankS. 1. It's not my question 2. It makes sense to read not only the question title but its contents as well before you mark something as a duplicate – zerkms Jul 08 '18 at 23:14
  • @zerkms Then the question title should be changed – Sheshank S. Jul 08 '18 at 23:14
  • 3
    @SheshankS. probably, but just because titles share few words does not mean questions are duplicates. Feel free to suggest a better title. – zerkms Jul 08 '18 at 23:15
  • @zerkms Ok, good idea – Sheshank S. Jul 08 '18 at 23:18
  • my only guess was that they're also using `lastIndexOf` for `String.endsWith()`, but that is not the case. – kyle Jul 08 '18 at 23:18
  • 2
    You may want to have a look here: https://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string/646643#comment63565350_35365431, and also the solution just below it (and the second answer as well). That being said, when you ask *"I was wondering why did they choose..."*, your question is automatically *opinion based*, since it is a choice of that team. However, if you're asking about the performance differences, I believe the question I linked is a good duplicate target (because of the two answers I mentioned). – Gerardo Furtado Jul 08 '18 at 23:40
  • @Luca - Firefox has them roughly equivalent. Chrome and Edge has IndexOf as much faster. – nixkuroi Jul 09 '18 at 00:00
  • @nixkuroi "Chrome and Edge has IndexOf as much faster." --- you're misinterpreting (the wrong) benchmark results. – zerkms Jul 09 '18 at 00:20
  • @zerkms - It's possible. Is JsPerf not a good performance test? https://jsperf.com/indexof-vs-lastindexof/16 – nixkuroi Jul 09 '18 at 19:09

1 Answers1

-3

MeasureThat is pretty cool for these types of questions, I created one for you here, might help you answer the question:

https://www.measurethat.net/Benchmarks/Show/3384/0/index-vs-lastindexof

According to that, indexOf is just a little bit faster. Of course, it does depend on what you are indexing.

IndexOf looks for the first occurrence and lastIndexOf returns to the last occurrence. That all said, their are much better functions around now like map.

To be honest though, if Google is using it there is probably a good reason.

  • This doesn't even answer the question. – Sheshank S. Jul 08 '18 at 23:28
  • "According to that, indexOf is just a little bit faster." --- according to that it shows the benchmark is poorly implemented and is optimised out by a vm. – zerkms Jul 08 '18 at 23:30
  • Erm, it kinda does 'indexOf is twice as fast in some cases.', that shows it isn't lol. I mean on top of that, the two are different functions. – Nicholas Griffin Jul 08 '18 at 23:31
  • "Erm, it kinda does 'indexOf is twice as fast in some cases.'," --- it does not, here the latter is 100 times faster, with 800M ops per second, which is indicative of optimisations: your test only shows that you can run nothing very fast. – zerkms Jul 08 '18 at 23:34
  • That's interesting, I think you actually got different results to me, I'm using Safari on a Mac, it's probably different between Chrome and Safari. It's not mine btw, just a site I know of. – Nicholas Griffin Jul 08 '18 at 23:37
  • You need to accumulate something in a benchmark. And yep - reliable benchmarking is hard. – zerkms Jul 08 '18 at 23:38