set array of objects

JavaScript performance comparison

Revision 13 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, p,
        size = 5,
        found = [],
        place = [];
   
    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 < 32000; i++) {
       
            done [ i ] = 0;
     }
   
    for ( i = 0; i < size -1; i++ ) {
   
           place [ i ] = 0;
    }
    for ( i = 0; i < size; ) {
   
        r = _.random ( 0, 31999 );
   
        if ( done [ r ] === 0 ) {
               
                do {
                   p = _.random ( 0, size - 1 );
                } while ( place [ p ] === 1 );
               
                place [ p ] = 1;
                s1 [ i ] = _.cloneDeep ( o );
                s2 [ p ] = _.cloneDeep ( o );
                s1 [ i ][ "id" ] = r;
                s2 [ p ][ "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 ++ ) {

    found [ i ] = false;
}

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