util.apply

JavaScript performance comparison

Test case created by zerious

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  window.data = {apiName:"Opportunity.Form1", eTag:"a82347skjdsf897q32wk441a69sdf873", label:"Form1", sections:[{apiName:"Section1", label:"Section1", columns:2, formSectionRows:[{formItems:[{formSubitems:[{apiName:"OwnerId", subitemType:"Field"}], label:"Owner ID"}, {formSubitems:[{apiName:"CloseDate", subitemType:"Field"}], label:"Close Date"}]}, {formItems:[{formSubitems:[{apiName:"Name", subitemType:"Field"}], label:"Opportunity Name"}, {formSubitems:[{apiName:"StageName", subitemType:"Field"}], label:"Stage"}]}, {formItems:[{formSubitems:[{apiName:"AccountId", subitemType:"Field"}], label:"Account Name"}, {formSubitems:[{apiName:"Probability", subitemType:"Field"}], label:"Probability (%)"}]}]}, {apiName:"Section2", label:"Section2", columns:2, formSectionRows:[{formItems:[{formSubitems:[{apiName:"Type", subitemType:"Field"}], label:"Opportunity Type"}, {formSubitems:[{apiName:"Amount", subitemType:"Field"}], label:"Amount"}]}, {formItems:[{formSubitems:[{apiName:"CreatedById", subitemType:"Field"}, {apiName:"CreatedDate", subitemType:"Field"}], label:"Created By"}, {formSubitems:[{apiName:"LastModifiedById", subitemType:"Field"}, {apiName:"LastModifiedDate", subitemType:"Field"}], label:"Last Modified By"}]}]}]};
  
  window.A = {
    isPlainObject: function(o){
        function isObjectObject(x) {
            return (typeof x === 'object' && x !== null)
                && Object.prototype.toString.call(x) === '[object Object]';
        }
        if (isObjectObject(o) === false) { return false; }
        if (typeof o.constructor !== 'function') { return false; }
        try {
            var p = o.constructor.prototype;
            if (isObjectObject(p) === false) { return false; }
            if (p.hasOwnProperty('isPrototypeOf') === false) {
                return false;
            }
        } catch (e) {}
        return true;
    },
    apply: function(baseObject, members, forceCopy, deepCopy) {
        if(members) {
            var value=null;
            for (var property in members) {
                var setValue=forceCopy||!baseObject.hasOwnProperty(property);
                if(setValue||deepCopy){
                    value=members[property];
                    if(deepCopy&&value!=undefined) {
                        var branchValue = null;
                        if (Array.isArray(value)) {
                            branchValue = baseObject[property] || [];
                        } else if (this.isPlainObject(value)) {
                            branchValue = baseObject[property] || {};
                        }
                        if (branchValue) {
                            baseObject[property] = this.apply(branchValue, value, forceCopy, deepCopy);
                            continue;
                        }
                    }
                    if(setValue) {
                        baseObject[property] = value;
                    }
                }
            }
        }
        return baseObject;
    }
  };
  
  window.B = {
      apply: function (to, from, clobber, deep) {
          if (deep) {
              return this.applyDeep(to, from, clobber);
          } else {
              var key;
              if (Array.isArray(from)) {
                  for (key = clobber ? 0 : to.length; key < from.length; key++) {
                      to[key] = from[key];
                  }
              } else {
                  if (clobber) {
                      Object.assign(to, from);
                  } else {
                      for (key in from) {
                          if (from.hasOwnProperty(key) && !to.hasOwnProperty(key)) {
                              to[key] = from[key];
                          }
                      }
                  }
              }
              return to;
          }
      },
      applyDeep: function (to, from, clobber) {
          var key, child;
          if (Array.isArray(from)) {
              for (key = 0; key < from.length; key++) {
                  child = from[key];
                  if (typeof child === "object" && child !== null) {
                      if (to[key]) {
                          this.applyDeep(to[key], child, clobber);
                      } else {
                          to[key] = this.copyDeep(child);
                      }
                  } else if (clobber || to.length <= key) {
                      to[key] = child;
                  }
              }
          } else {
              for (key in from) {
                  if (from.hasOwnProperty(key)) {
                      child = from[key];
                      if (typeof child === "object" && child !== null) {
                          if (to[key]) {
                              this.applyDeep(to[key], child, clobber);
                          } else {
                              to[key] = this.copyDeep(child);
                          }
                      } else if (clobber || !to.hasOwnProperty(key)) {
                          to[key] = child;
                      }
                  }
              }
          }
          return to;
      },
      copyDeep: function (value) {
          if (typeof value === "object") {
              var clone, key, child;
              if (Array.isArray(value)) {
                  clone = Array(value.length);
                  for (key = 0; key < value.length; key++) {
                      clone[key] = typeof (child = value[key]) === "object" ? this.copyDeep(child) : child;
                  }
              } else {
                  clone = {};
                  for (key in value) {
                      if (value.hasOwnProperty(key)) {
                          clone[key] = typeof (child = value[key]) === "object" ? this.copyDeep(child) : child;
                      }
                  }
              }
              value = clone;
          }
          return value;
      }
  };

};
</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
Current
return A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true)
  && A.apply({},data,true,true) && A.apply({},data,true,true);
pending…
Proposed
return B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data)
  && B.copyDeep(data) && B.copyDeep(data);
pending…

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

Compare results of other browsers

0 Comments