unusual Array some

JavaScript performance comparison

Test case created by WebReflection

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // 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);
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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

Compare results of other browsers

Revisions

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

1 Comment