substring-vs-indexOf-vs-lodash-indexOf-vs-lodash-contains

Benchmark created by jrencz on


Description

What is the fastest way to check if string starts with certain substring:

substring vs indexOf vs _.indexOf vs _().indexOf

Preparation HTML

<script src='https://rawgithub.com/lodash/lodash/2.4.1/dist/lodash.min.js'></script>

Setup

var text = 'aaaFooBar';

Test runner

Ready to run.

Testing in
TestOps/sec
substring
if (text.substring(0,3) === 'aaa') { return true}
ready
indexOf
if (text.indexOf("aaa") === 0) { return true}
ready
_.indexOf(str, val)
if(_.indexOf(text, 'aaa') === 0) { return true}
ready
_(str).indexOf(val)
if(_(text).indexOf('aaa') === 0) { return true}
ready

Revisions

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