set array of objects

JavaScript performance comparison

Revision 6 of this test case created by

Preparation code

<script src="https://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 ] = 0;
          found [ i ] = false;
   }
  
  for ( i = 0; i < size; ) {
  
      r = _.random ( 0, size -1 );
  
      if ( done [ r ] === 0 ) {
              s1 [ r ] = _.cloneDeep ( o );
              s1 [ r ][ "id" ] = i;
              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 ) {
              s2 [ r ] = _.cloneDeep ( o );
              s2 [ r ][ "id" ] = i;
              done [ r ] = 1;
              i = i + 1;
     }        
  }
  
  
  

};
</script>

Preparation code output

<script> var lodash = _.noConflict(); </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
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.

0 Comments