set array of objects

JavaScript performance comparison

Revision 3 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 = 50,
        found = [];
   
    var o = {
   
        "name": "John",
        "married": true,
        "alias": [ "Jo", "Johny", "Amy" ],
        "dob": "1999/12/12",
        "tob": "11:30",
        "credit": "USD10",
        "address": {
   
            "city"  : "Seattle",
            "zip"   : 98109,
            "state" : "WA",
            "street": "Brown Street",
            "house_no": 123,
        },
        "pets": [ "cat", "dog", "foal", "goat", "bird", "rabbit"],
        "age": 22,
        "height": 5.4,
        "title": "Dr.",
        "children": [
           {
              "name": "Emily",
              "age" : 3,
              "tob" : "12:00",
              "dob" : "2000/12/12",
              "address": {
               
                  "house": 1,
                  "street": "Brown Street",
                  "City": "Seattle",
                  "State": "WA",
                  "Zip":  98019
              }
           },
           {
              "name": "George",
              "age" : 3,
              "tob" : "12:00",
              "dob" : "2000/12/12",
              "address": {
               
                  "house": 1,
                  "street": "Brown Street",
                  "City": "Seattle",
                  "State": "WA",
                  "Zip":  98019
              }
            },
            {
              "name": "Anita",
              "age" : 3,
              "tob" : "12:00",
              "dob" : "2000/12/12",
              "address": {
               
                  "house": 1,
                  "street": "Brown Street",
                  "City": "Seattle",
                  "State": "WA",
                  "Zip":  98019
              }
           }
        ]
    };
   
     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 [ i ] = {};
            s1 [ i ][ "name" ] = _.cloneDeep ( o ["name"] );
            s1 [ i ][ "married" ] = _.cloneDeep ( o ["married"] );
            s1 [ i ][ "alias" ] = _.cloneDeep ( o ["alias"] );
            s1 [ i ][ "dob" ] = _.cloneDeep ( o ["dob"] );
            s1 [ i ][ "tob" ] = _.cloneDeep ( o ["tob"] );
            s1 [ i ][ "credit" ] = _.cloneDeep ( o ["credit"] );
            s1 [ i ][ "address" ] = _.cloneDeep ( o ["address"] );
            s1 [ i ][ "city" ] = _.cloneDeep ( o ["city"] );
            s1 [ i ][ "pets" ] = _.cloneDeep ( o ["pets"] );
            s1 [ i ][ "children" ] = _.cloneDeep ( o ["children"] );
            s1 [ i ][ "title" ] = _.cloneDeep ( o ["title"] );
            s1 [ i ][ "height" ] = _.cloneDeep ( o ["height"] );
            s1 [ i ][ "age" ] = _.cloneDeep ( o ["age"] );
            s1 [ 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 ) {
     
            s2 [ i ] = {};
            s2 [ i ][ "name" ] = _.cloneDeep ( o ["name"] );
            s2 [ i ][ "married" ] = _.cloneDeep ( o ["married"] );
            s2 [ i ][ "alias" ] = _.cloneDeep ( o ["alias"] );
            s2 [ i ][ "dob" ] = _.cloneDeep ( o ["dob"] );
            s2 [ i ][ "tob" ] = _.cloneDeep ( o ["tob"] );
            s2 [ i ][ "credit" ] = _.cloneDeep ( o ["credit"] );
            s2 [ i ][ "address" ] = _.cloneDeep ( o ["address"] );
            s2 [ i ][ "city" ] = _.cloneDeep ( o ["city"] );
            s2 [ i ][ "pets" ] = _.cloneDeep ( o ["pets"] );
            s2 [ i ][ "children" ] = _.cloneDeep ( o ["children"] );
            s2 [ i ][ "title" ] = _.cloneDeep ( o ["title"] );
            s2 [ i ][ "height" ] = _.cloneDeep ( o ["height"] );
            s2 [ i ][ "age" ] = _.cloneDeep ( o ["age"] );
            s2 [ 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…

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