set array of objects

JavaScript performance comparison

Revision 5 of this test case created

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.js">
</script>
<script>
  var lodash = _.noConflict();
</script>
<script>
Benchmark.prototype.setup = function() {
    var _ = window.lodash;
    var s1 = [],
        s2 = [],
        done = [],
        i, j, match, r, size = 5,
        found = [];
   
    var o = {
   
      "name": "John",
      "married": true,
      "alias": ["Jo", "Johny"],
      "dob": "1999/12/12",
      "tob": "11:30",
      "credit": "USD10",
      "address": {
   
        "city": "Seattle",
        "zip": 98109
      },
      "pets": ["cat", "dog"]
    };
   
    for (i = 0; i < size; i++) {
   
      done[i] = _.random(0,size-1);
      found[i] = false;
    }
   
    for (i = 0; i < size; i++) {
      s1[i] = _.cloneDeep(o);
      s2[done[i]] = _.cloneDeep(o);
      s1[i]["id"] = _.random(0, 32000);
      s2[done[i]]["id"] = s1[i]["id"];
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
double loop with caching
for (i = 0; i < size; i++) {

  match = false;

  for (j = 0; j < size; j++) {

    if (found[j] === true) {
      continue;
    }
    if (_.isEqual(s1[i], s2[j])) {

      found[j] = true;
      match = true;
      break;
    }
  }

  if (match === false) {

    break;
  }
}
pending…
simple double loop
for (i = 0; i < size; i++) {

  match = false;

  for (j = 0; j < size; j++) {

    if (_.isEqual(s1[i], s2[j])) {

      match = true;
      break;
    }
  }

  if (match === false) {

    break;
  }
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment