regexp vs indexOf (v109)

Revision 109 of this benchmark created on


Preparation HTML

<script>
  var str = "BabylonCSG\n==========\n\nConstructive Solid Geometry in BABYLON.js, based on csg.js\n\n![](http://evanw.github.com/csg.js/image.png)\n\nConstructive Solid Geometry (CSG) is a modeling technique that uses Boolean operations like union and intersection to combine 3D solids. This library implements CSG operations on meshes elegantly and concisely using BSP trees, and is meant to serve as an easily understandable implementation of the algorithm. All edge cases involving overlapping coplanar polygons in both solids are correctly handled.\n\nThis library is an adaptation for BABYLON.js meshes, original documentation on csg.js can be found here :\nhttps://github.com/evanw/csg.js/\n\n#Example\n\n\tvar a = BABYLON.Mesh.CreateBox(\"box\", 500, scene);\n\tvar b = BABYLON.Mesh.CreateBox(\"box\", 500, scene);\n\n\ta.position.y += 500;\n\tb.position.y += 250;\n\tb.rotation.y += Math.PI/8;\n\n\tvar aCSG = BABYLON.CSG.FromMesh(a);\n\tvar bCSG = BABYLON.CSG.FromMesh(b);\n\n\tvar subCSG = bCSG.subtract(aCSG);\n\n\t// Disposing original meshes since we don't want to see them on the scene\n\ta.dispose();\n\tb.dispose();\n\n\tsubCSG.toMesh(\"csg\", new BABYLON.StandardMaterial(\"mat\", scene), scene);\n\n![](http://f.cl.ly/items/1f1v2Y2O1Y1e1I3f2j2I/Capture.PNG)\n\n#Multi-Material\nYou may want to keep one subMesh for each mesh you used CSG on. This library allows you to do so :\n\n\tvar a = BABYLON.Mesh.CreateBox(\"box\", 500, scene);\n\tvar b = BABYLON.Mesh.CreateBox(\"box\", 500, scene);\n\n\ta.position.y += 500;\n\tb.position.y += 250;\n\tb.rotation.y += Math.PI/8;\n\n\tvar aCSG = BABYLON.CSG.FromMesh(a);\n\tvar bCSG = BABYLON.CSG.FromMesh(b);\n\n\tvar subCSG = bCSG.subtract(aCSG);\n\n\t// Disposing original meshes since we don't want to see them on the scene\n\ta.dispose();\n\tb.dispose();\n\n\t// Set up a MultiMaterial\n\tvar multiMat = new BABYLON.MultiMaterial(\"multiMat\", scene);\n\tvar mat0 = new BABYLON.StandardMaterial(\"mat0\", scene);\n\tvar mat1 = new BABYLON.StandardMaterial(\"mat1\", scene);\n\n\tmat0.diffuseColor.copyFromFloats(0.8, 0.2, 0.2);\n\tmat1.diffuseColor.copyFromFloats(0.2, 0.8, 0.2);    \n\n\t// Submeshes are built in order : mat0 will be for the first cube, and mat1 for the second\n\tmultiMat.subMaterials.push(mat0, mat1);\n\n\t// Last parameter to true means you want to build 1 subMesh for each mesh involved\n\tsubCSG.toMesh(\"csg\", multiMat, scene, true);\n\n![](http://f.cl.ly/items/3G2A3B002C2k0P162A0D/Capture.PNG)\n";
function getMatchIndexes1(str, toMatch) {
    var re = new RegExp(toMatch, "ig"),
        indexMatches = [], match;

    while (match = re.exec(str)) {
        indexMatches.push(match.index);
    }

    return indexMatches;
}
function getMatchIndexes2(str, toMatch) {
    var toMatchLength = toMatch.length,
        indexMatches = [], match,
        i = 0;

    while ((match = str.toLowerCase().indexOf(toMatch.toLowerCase(), i)) > -1) {
        indexMatches.push(match);
        i = match + toMatchLength;
    }

    return indexMatches;
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Regexp
getMatchIndexes1(str, "BABYLON");
ready
indexOf
getMatchIndexes2(str, "BABYLON");
ready

Revisions

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