startsWith() vs regex.test()

Benchmark created on


Setup

const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const charactersLength = characters.length;

function makeString() {
    let result = '';
    for (let counter = 0, result = ''; counter < 20; counter++) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

Test runner

Ready to run.

Testing in
TestOps/sec
regex.test()
console.log(/^foobar/.test(makeString()));
ready
startsWith()
console.log(makeString().startsWith('foobar'));
ready

Revisions

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