indexOf vs substring (v2)

Revision 2 of this benchmark created by James on


Description

tests whether it is faster to check indexOf at the end of a string rather than getting the substring

Setup

var str = "blank";
    var str2 = "match";
    var str3 = "notmatch";
    
    var needle = "mat";
    
    var bool1, bool2, bool3;

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
bool1 = str.indexOf(needle) === 0;
bool2 = str2.indexOf(needle) === 0;
bool3 = str3.indexOf(needle) === 0;
ready
substring
bool1 = str.substring(0,3) === needle;
bool2 = str2.substring(0,3) === needle;
bool3 = str3.substring(0,3) === needle;
ready

Revisions

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

  • Revision 1: published by Shane O'Sullivan on
  • Revision 2: published by James on