#2 comparison (v3)

Revision 3 of this benchmark created on


Description

comparing two simple approaches to string searching in a list

Preparation HTML

<div id="result"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var list = [];
    for (var i=0; i < 5000; i++)
    {
        var rand_string = '';
        for (var j=0; j < 6; j++)
        {
             var seed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
             rand_string += seed[Math.floor(Math.random()*62)];
        }
        list.push(rand_string);
    }
    list.push('stljs');

Test runner

Ready to run.

Testing in
TestOps/sec
simple for each
var result = 'false';
for (var i=0; i < list.length; i++)
{
  if (/stljs/.test(list[i]))
  {
    result = 'true';
    break;
  }
}
$('#result').text(result);
ready
test against join
var result = 'false';
result = /,stljs/.test(list.join(',')) ? 'true' : 'false'
$('#result').text(result);
ready
indexOf
result = list.indexOf('stljs') !== -1
$('#result').text(result);
ready

Revisions

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