set array of objects

JavaScript performance comparison

Revision 8 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 = [],
        s3 = [], s4 = [],
        done = [],
        i, j, match, r, size = 10,
        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"];
    }
   
   
    for ( i = 0; i < size; i++) {
   
        done [ i ] = 0;
    }
   
    for ( i = 0; i < size; ) {
   
        r = _.random ( 0, size -1 );
   
        if ( done [ r ] === 0 ) {
            s3 [ i ] = _.cloneDeep ( o );
            s3 [ i ][ "id" ] = r;
            done [ r ] = 1;
            i = i + 1;
        }
    }
   
    for ( i = 0; i < size; i++) {
   
        done [ i ] = 0;
    }
   
    for ( i = 0; i < size; ) {
   
        r = _.random ( 0, size -1 );
   
        if ( done [ r ] === 0 ) {
            s4 [ i ] = _.cloneDeep ( o );
            s4 [ i ][ "id" ] = r;
            done [ r ] = 1;
            i = i + 1;
        }
    }
   
};
</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…
input with no holes, strategy - caching
for (i = 0; i < size; i++) {

    match = false;

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

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

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

    if (match === false) {

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

  match = false;

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

    if (_.isEqual(s3[i], s4[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