string indexOf vs for loop

Benchmark created by Benjamin De Cock on


Setup

var str = "hello";

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
var index = str.indexOf("o");
ready
for() loop
var index = -1;
for (var i = 0, l = str.length; i < l; i++) {
  if (str[i] === "o") {
    index = i;
    break;
  }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.