extend vs newextend (v2)

Revision 2 of this benchmark created on


Setup

/**
     * A extends B
     *
     * util.inherits works only with objects derived from Object
     *
     * @return {Object} Extended object
     */
    function extend(a, b, noClone) { // A extends B
      if(!a || !noClone){
        a = clone(a);
      }
      for (var key in b) {
        if (typeof a[key] === 'undefined' || !(typeof a[key] === 'object' && typeof a[key].length === 'undefined') && typeof a[key] !== 'function') { // Simple types
          a[key] = b[key];
        } else { // Complex types
          a[key] = extend(a[key], b[key]);
        }
      }
    
      return a;
    };
    
    /**
     * Function for creating a clone of an object
     * 
     * @param o {Object}  object to clone
     * @return {Object}
     */
    function clone(o){
      var c = {};
      for(var i in o){
        c[i] = o[i];
      }
      return c;
    }
    /**
     * A extends B
     *
     * util.inherits works only with objects derived from Object
     *
     * @return {Object} Extended object
     */
    function newextend(a, b, noClone) { // A extends B
      a = a || {};
      
      if(typeof a !== 'object' || typeof a.length !== 'undefined'){
        return b;
      }
      
      if(typeof b !== 'object' || typeof b.length !== 'undefined'){
        return b;
      }
      
      if(!noClone){
        a = newclone(a);
      }
      
      var bk = Object.keys(b);
      for(var i = 0, c = bk.length; i < c; i++){
        var key = bk[i];
        if (!a.hasOwnProperty(key) || !(typeof b[key] === 'object' && typeof b[key].length === 'undefined') && typeof b[key] !== 'function') { // Simple types
          a[key] = b[key];
        } else { // Complex types
          a[key] = newextend(a[key], b[key]);
        }
      }
    
      return a;
    };
    
    /**
     * Function for creating a clone of an object
     * 
     * @param o {Object}  object to clone
     * @return {Object}
     */
    function newclone(o){
      var c = {};
      var h = Object.keys(o);
      for(var i = 0, co = h.length; i < co; i++){
        c[h[i]] = o[h[i]];
      }
      return c;
    }
    var a = {};
    var b = {};
    for(var i = 0; i < 1000; i++){
    if(i%2){
    a['lo'+i] = 'va1-'+i;
    }
    if(i%5){
    a['le'+i] = {};
    for(var s = 0; s <50; s++){
    a['le'+i][s] = 'se'+s;
    }
    }
    if(i%6){
    b['le'+i] = {};
    for(var s = 0; s <50; s++){
    b['le'+i][s] = 'ses'+s;
    }
    }
    b['lo'+i] = 'va2-'+i;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
oldextend
extend(a, b);
ready
newextend
newextend(a, b);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.