Array vs object lookups

JavaScript performance comparison

Test case created by usmanity

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arrayData = [
      {
          longUrl: "http://usmanity.com",
          shortUrl: "u"
      },
      {
          longUrl: "https://github.com/nodesource/distributions/blob/master/README.md#debinstall",
          shortUrl: "a7f3",
      },
      {
          longUrl: "http://yargs.js.org/docs/",
          shortUrl: "0fd9",
      },
      {
          longUrl: "http://expressjs.com/en/api.html#app.post.method",
          shortUrl: "7f8d",
      }
  ];
  
  var objectData = {
      "u": "http://usmanity.com",
      "a7f3": "https://github.com/nodesource/distributions/blob/master/README.md#debinstall",
      "0fd9": "http://yargs.js.org/docs/",
      "7f8d": "http://expressjs.com/en/api.html#app.post.method"
  };
  
  function findInArray(lookup) {
      for (var i = 0; i < arrayData.length; i++) {
          if (arrayData[i].shortUrl === lookup) {
              return true;
          }
      }
  }
  
  function findInObject(lookup) {
      return objectData[lookup];
  }

};
</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
Array Lookup
findInArray('u');
findInArray('0fd9');
findInArray('404');
pending…
Object lookup
findInObject('u');
findInObject('0fd9');
findInObject('404');
pending…

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

Compare results of other browsers

0 Comments