#2 comparison (v5)

Revision 5 of this benchmark created on


Description

comparing two simple approaches to string searching in a list

Preparation HTML

<div id="result"></div>

Setup

var list = [],
      result = false;
    for (var i = 0; i < 50000; 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('stljsstljsstljsstljsstljsstljs');

Teardown


    document.getElementById('result').innerHTML = result.toString();
  

Test runner

Ready to run.

Testing in
TestOps/sec
simple for each
for (var i = 0; i < list.length; i++) {
  if (/stljsstljsstljsstljsstljsstljs/.test(list[i])) {
    result = true;
    break;
  }
}
ready
test against join
result = /,stljsstljsstljsstljsstljsstljs/.test(list.join(','));
ready
indexOf
result = list.indexOf('stljsstljsstljsstljsstljsstljs') > -1;
ready
Array.some()
result = list.some(function(element, index, array) {
  return element === 'stljsstljsstljsstljsstljsstljs';
});
ready
while
var index = list.length;
while (index) {
  index--;
  result = list[index] === 'stljsstljsstljsstljsstljsstljs';
  if (result) {
    break;
  }
}
ready
Substr
result = ~list.toString().indexOf('stljsstljsstljsstljsstljsstljs')
ready
result = ~list.toString().match(/stljsstljsstljsstljsstljsstljs/)
ready
result = /stljsstljsstljsstljsstljsstljs/.test(list.toString())
ready

Revisions

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