JavaScript string character finder

JavaScript performance comparison

Test case created by Tomalak


finds the positions of a certain character in a string

Preparation code

Benchmark.prototype.setup = function() {
    var string = "scissors";
    var char = "s";
    function Tomalak(str, char) {
      return str.split("").map(function(c, i) {
        if (c == char) return i;
      }).filter(function(v) {
        return v >= 0;
    function Phrogz(string, substring) {
      var a = [],
          i = -1;
      while ((i = string.indexOf(substring, i + 1)) >= 0) a.push(i);
      return a;
    function vcsjones(str, char) {
      var indices = [];
      for (var i = 0; i < str.length; i++) {
        if (str[i] === "s") indices.push(i);

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
var result = Tomalak(string, char);
var result = Phrogz(string, char);
var result = vcsjones(string, char);

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Gavin Kistner commented :

See also the tests

Comment form temporarily disabled.

Add a comment