angular merge

Benchmark created on


Setup

/**
   * toString ref.
   */
  
  var toString = Object.prototype.toString;
  var has = Object.prototype.hasOwnProperty;
  
  /**
   * Return the type of `val`.
   *
   * @param {Mixed} val
   * @return {String}
   * @api public
   */
  
  function type(val){
    switch (toString.call(val)) {
      case '[object Function]': return 'function';
      case '[object Date]': return 'date';
      case '[object RegExp]': return 'regexp';
      case '[object Arguments]': return 'arguments';
      case '[object Array]': return 'array';
      case '[object String]': return 'string';
    }
  
    if (val === null) return 'null';
    if (val === undefined) return 'undefined';
    if (val && val.nodeType === 1) return 'element';
    if (val === Object(val)) return 'object';
  
    return typeof val;
  };
  
  function cmerge (a, b) {
    var t = type(b);
    // set simple types
    if (t !== 'object' && t !== 'array') return b;
  
    // if they're different types just return b
    if (type(a) !== t) return b;
  
    // make the arrays the same length
    if (t === 'array') a.splice(b.length);
    
    // remove any old properties
    // TODO is there a way not to make this thing O(2N)?
    if (t === 'object') {
      for (var key in a) {
        if (key.charAt && key.charAt(0) === '$') continue;
        if (!b[key]) delete a[key];
      }
    }
    
    for (var key in b) {
      if (has.call(b, key)) a[key] = cmerge(a[key], b[key]);
    }
    return a;
  }
  
  function nmerge (a, b) {
    var t = type(b);
    // set simple types
    if (t !== 'object' && t !== 'array') return b;
  
    // if they're different types just return b
    if (type(a) !== t) return b;
  
    // make the arrays the same length
    if (t === 'array') a.splice(b.length);
    
    // remove any old properties
    // TODO is there a way not to make this thing O(2N)?
    if (t === 'object') {
      for (var key in a) {
        if (key.charAt && key.charAt(0) === '$') continue;
        if (!b[key]) delete a[key];
      }
    }
    
    for (var key in b) {
      a[key] = nmerge(a[key], b[key]);
    }
    return a;
  }
  
  function smerge (a, b) {
    var t = type(b);
    // set simple types
    if (t !== 'object' && t !== 'array') return b;
  
    // if they're different types just return b
    if (type(a) !== t) return b;
  
    // make the arrays the same length
    if (t === 'array') a.splice(b.length);
    
    // remove any old properties
    // TODO is there a way not to make this thing O(2N)?
    if (t === 'object') {
      for (var key in a) {
        if (key.charAt && key.charAt(0) === '$') continue;
        if (!b[key]) delete a[key];
      }
    }
    
    var val;
    var vt;
    for (var key in b) {
      val = b[key];
      vt = type(val);
      if (vt !== 'object' && vt !== 'array') a[key] = b[key];
      else a[key] = smerge(a[key], b[key]);
    }
    return a;
  }
  
  var m1 = {"data":[{"author":{"href":"http://oc-api-mock.herokuapp.com/users/98c9b463-5c83-4907-a7f8-ecff93f297d8"},"message":"asdfasdf","date":"2013-12-21T19:22:32.519Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/e509f9be-0ee6-41fc-b074-40f7c7f670dc"},"message":"asdfasdf","date":"2013-12-21T19:22:36.165Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/0a3abd2c-0ce8-4613-8eda-0d19397a5fb2"},"message":"asdfasdf","date":"2013-12-21T19:22:39.753Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/b49a62ca-cc90-43fa-8076-6290083f5273"},"message":"asdfasdf","date":"2013-12-21T19:22:42.894Z"}],"comment":{"method":"POST","action":"http://oc-api-mock.herokuapp.com/hub/events/1554/comments","input":{"message":{"type":"text","required":true}}},"root":{"href":"http://oc-api-mock.herokuapp.com"},"href":"http://oc-api-mock.herokuapp.com/hub/events/1554/comments"};
  var m2 = {"data":[{"author":{"href":"http://oc-api-mock.herokuapp.com/users/98c9b463-5c83-4907-a7f8-ecff93f297d8"},"message":"asdfasdf","date":"2013-12-21T19:22:32.519Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/e509f9be-0ee6-41fc-b074-40f7c7f670dc"},"message":"asdfasdf","date":"2013-12-21T19:22:36.165Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/0a3abd2c-0ce8-4613-8eda-0d19397a5fb2"},"message":"asdfasdf","date":"2013-12-21T19:22:39.753Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/b49a62ca-cc90-43fa-8076-6290083f5273"},"message":"asdfasdf","date":"2013-12-21T19:22:42.894Z"},{"author":{"href":"http://oc-api-mock.herokuapp.com/users/b49a62ca-cc90-43fa-8076-6290083f5273"},"message":"asdfasdf","date":"2013-12-21T19:22:42.894Z"}],"comment":{"method":"POST","action":"http://oc-api-mock.herokuapp.com/hub/events/1554/comments","input":{"message":{"type":"text","required":true}}},"root":{"href":"http://oc-api-mock.herokuapp.com"},"href":"http://oc-api-mock.herokuapp.com/hub/events/1554/comments"};

Test runner

Ready to run.

Testing in
TestOps/sec
hasProperty
cmerge(m1, m2);
ready
no check
nmerge(m1, m2);
ready
scalar check
smerge(m1, m2);
ready

Revisions

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