unusual Array some

Benchmark created by WebReflection on


Setup

// only once
    var arr = [];
    var who = Math.random();
    var magicIndex = Math.round(
      Math.random() * 0x79 + 0x79
    );
    for(var i = 0; i < 0xFF; i++) {
      arr[i] = {name: Math.random()};
    }
    arr[magicIndex].name = who;
    var result = arr[magicIndex];
    
    function verify(user) {
      if (user !== result) {
        throw 'epic fail';
      }
    }
    
    function findUser(user, i) {
      return user.name == this && ~(findUser.index = i);
    }
    
    function findUserRegExp(user, i) {
      return user.name == this && /\d+/.test(i);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
just some
// cheating here!
verify(arr.some(function (user) {
  return user.name == this;
}, who) && arr[magicIndex]);
ready
outer scope each time
var index;
verify(arr.some(function (user, i) {
  index = i;
  return user.name == this;
}, who) && arr[index]);
ready
outer scope once
var index;
verify(arr.some(function (user, i) {
  return user.name == this && ~(index = i);
}, who) && arr[index]);
ready
recycled function
verify(arr.some(findUser, who) && arr[findUser.index]);
ready
index and RegExp
verify(arr.some(findUserRegExp, who) && arr[RegExp['$&']]);
ready

Revisions

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

  • Revision 1: published by WebReflection on