underscore icon indicating copy to clipboard operation
underscore copied to clipboard

Deep Extend and Deep Copy

Open jscheel opened this issue 15 years ago • 28 comments

Feature Request: Would it be possible to either have a boolean parameter on _.extend() and _.copy() to make them deep, or to have separate deep methods?

jscheel avatar Mar 25 '11 12:03 jscheel

I'm afraid that there are no really good semantics for a deep copy operation in JavaScript. Existing implementations have various bugs, like mutating nested arrays, and failing to copy nested Date objects. If you'd like to propose this feature, you'll have to accompany it with a bulletproof implementation.

jashkenas avatar Apr 15 '11 21:04 jashkenas

True, deep copies are definitely messy in javascript. There has to be some point where you say that the benefits outweigh the detriments though, right? Having people constantly write crummy deep-copy methods that don't work as expected is worse than using a deep copy method that has been carefully coded, but has well-documented limitations. For example, this guy's solution is great: http://stackoverflow.com/questions/728360/copying-an-object-in-javascript/728694#728694

He covers most of the bases, and documents what it won't handle correctly. He assumes the object contains only these types: Object, Array, Date, String, Number, and Boolean, and he assumes that any objects or arrays will only contain the same.

jscheel avatar Apr 16 '11 15:04 jscheel

Yep -- and even that guy's solution really isn't good enough. If we can't implement it correctly, we shouldn't be implementing it.

jashkenas avatar Apr 16 '11 15:04 jashkenas

But is it really better for users to roll their own, probably even more broken, implementation?

jscheel avatar Apr 16 '11 18:04 jscheel

No -- it's better for users not to deep copy in JavaScript. You can usually find a way to accomplish the same end without having to have a robust deep copy function ... by knowing the structure of the object you want to copy in advance.

jashkenas avatar Apr 16 '11 19:04 jashkenas

Ah, so you are suggesting the user hydrates a new instance of the object with the necessary values? I can see that.

jscheel avatar Apr 16 '11 19:04 jscheel

Jquery.extend has deep option.

lexer avatar Apr 18 '11 12:04 lexer

+1 for the deep option, regardless of how difficult it is to implement.

visualmotive avatar Aug 18 '11 19:08 visualmotive

+2 for this - it is not difficult to implement, it is a matter of principle (i.e. not implementing a less-than-perfect solution). However, as mentioned above, it is used in the jQuery library and quite useful in all but a few edge cases. It would be nice to have it available in a light-weight library like this.

fshost avatar Oct 12 '11 19:10 fshost

@kmalakoff has written an implementation, you should give him some feedback! :)

yuchi avatar Oct 12 '11 21:10 yuchi

You could even merge my original _.cloneToDepth with _clone and just add a depth parameter....

  // Create a duplicate of a container of objects to any zero-indexed depth.
  _.cloneToDepth = _.clone = function(obj, depth) {
    if (typeof obj !== 'object') return obj;
    var clone = _.isArray(obj) ? obj.slice() : _.extend({}, obj);
    if (!_.isUndefined(depth) && (depth > 0)) {
      for (var key in clone) {
        clone[key] = _.clone(clone[key], depth-1);
      }
    }
    return clone;
  };

kmalakoff avatar Oct 12 '11 22:10 kmalakoff

Also, I wrote _.own and _.disown which introduce a convention for ownership (either pairs of retain/release or clone/destroy). It only recurses one level down, but I suppose there could be an option added for total recursion (I'd want to see a use case!).

  _.own = function(obj, options) {
    if (!obj || (typeof(obj)!='object')) return obj;
    options || (options = {});
    if (_.isArray(obj)) {
      if (options.share_collection) { _.each(obj, function(value) { _.own(value, {prefer_clone: options.prefer_clone}); }); return obj; }
      else { var a_clone =  []; _.each(obj, function(value) { a_clone.push(_.own(value, {prefer_clone: options.prefer_clone})); }); return a_clone; }
    }
    else if (options.properties) {
      if (options.share_collection) { _.each(obj, function(value, key) { _.own(value, {prefer_clone: options.prefer_clone}); }); return obj; }
      else { var o_clone = {}; _.each(obj, function(value, key) { o_clone[key] = _.own(value, {prefer_clone: options.prefer_clone}); }); return o_clone; }
    }
    else if (obj.retain) {
      if (options.prefer_clone && obj.clone) return obj.clone();
      else obj.retain();
    }
    else if (obj.clone) return obj.clone();
    return obj;
  };

  _.disown = function(obj, options) {
    if (!obj || (typeof(obj)!='object')) return obj;
    options || (options = {});
    if (_.isArray(obj)) {
      if (options.clear_values) { _.each(obj, function(value, index) { _.disown(value); obj[index]=null; }); return obj; }
      else {
        _.each(obj, function(value) { _.disown(value); });
        obj.length=0; return obj;
      }
    }
    else if (options.properties) {
      if (options.clear_values) { _.each(obj, function(value, key) { _.disown(value); obj[key]=null; }); return obj; }
      else {
        _.each(obj, function(value) { _.disown(value); });
        for(key in obj) { delete obj[key]; }
        return obj;
      }
    }
    else if (obj.release) obj.release();
    else if (obj.destroy) obj.destroy();
    return obj;
  };

kmalakoff avatar Oct 12 '11 22:10 kmalakoff

And if you want a general purpose, extensible clone:

  // Create a duplicate of all objects to any zero-indexed depth.
  _.deepClone = function(obj, depth) {
    if (typeof obj !== 'object') return obj;
    if (_.isString(obj)) return obj.splice();
    if (_.isDate(obj)) return new Date(obj.getTime());
    if (_.isFunction(obj.clone)) return obj.clone();
    var clone = _.isArray(obj) ? obj.slice() : _.extend({}, obj);
    if (!_.isUndefined(depth) && (depth > 0)) {
      for (var key in clone) {
        clone[key] = _.deepClone(clone[key], depth-1);
      }
    }
    return clone;
  };

kmalakoff avatar Oct 12 '11 23:10 kmalakoff

I'm unconvinced that this is a good idea until a use case is given where a deep copy is actually the best solution. I think it will be hard to find one.

michaelficarra avatar Oct 13 '11 02:10 michaelficarra

I wasn't completely satisfied with my response yesterday (shouldn't write code after midnight)...I've come up with two versions (_.cloneToDepth basically just clones the container, retaining references to original objects and _.deepClone which copies the instances):

  // Create a duplicate of a container of objects to any zero-indexed depth.
  _.cloneToDepth = _.containerClone = _.clone = function(obj, depth) {
    if (!obj || (typeof obj !== 'object')) return obj;  // by value
    var clone;
    if (_.isArray(obj)) clone = Array.prototype.slice.call(obj);
    else if (obj.constructor!=={}.constructor) return obj; // by reference
    else clone = _.extend({}, obj);
    if (!_.isUndefined(depth) && (depth > 0)) {
      for (var key in clone) {
        clone[key] = _.clone(clone[key], depth-1);
      }
    }
    return clone;
  };

  // Create a duplicate of all objects to any zero-indexed depth.
  _.deepClone = function(obj, depth) {
    if (!obj || (typeof obj !== 'object')) return obj;  // by value
    else if (_.isString(obj)) return String.prototype.slice.call(obj);
    else if (_.isDate(obj)) return new Date(obj.valueOf());
    else if (_.isFunction(obj.clone)) return obj.clone();
    var clone;
    if (_.isArray(obj)) clone = Array.prototype.slice.call(obj);
    else if (obj.constructor!=={}.constructor) return obj; // by reference
    else clone = _.extend({}, obj);
    if (!_.isUndefined(depth) && (depth > 0)) {
      for (var key in clone) {
        clone[key] = _.deepClone(clone[key], depth-1);
      }
    }
    return clone;
  };

As @michaelficarra points out, the use cases may be unclear. Personally, I use:

  1. _.own/_.disown when I want to share objects that have complex lifecycles and/or ownership models (like reference counting to handle clean up properly)
  2. I used _.cloneToDepth/_.containerClone (rarely!) when I had complex, nested options for a function.
  3. I've never needed a _.deepClone, but suppose it could be useful as a general purpose _.clone method if you are writing a generic function that flexibly supports types (eg. I don't care what you pass me, but I'm going to modify it and don't want to have side-effects on the original - although strings are a special immutable case).

I've submitted the code and tests here: https://github.com/kmalakoff/underscore-awesomer/commit/0cf6008f16ad6e6daf60caf456021693ef33fec5

kmalakoff avatar Oct 13 '11 06:10 kmalakoff

Note: those stuck looking for a one-line, incomplete deep-clone for simple objects with predictable semantics can just JSON.parse(JSON.stringify(object)).

adamhooper avatar Aug 07 '12 18:08 adamhooper

@adamhooper Does this method tend to lose properties or something? Why is it incomplete?

diversario avatar Oct 17 '12 14:10 diversario

@diversario It won't copy the object prototype, and it won't copy functions. That applies recursively--so it won't copy nested objects' prototypes or functions properly either.

In particular: it won't properly copy any Date in your object tree. And if you want to fix it to work with Dates, well, you're simply addressing a small symptom of a much larger problem.

adamhooper avatar Oct 17 '12 14:10 adamhooper

Oh, right. I mostly use it to break reference to things like "template" objects, so I haven't ran into anything like that. But I see the need for real deep copy.

diversario avatar Oct 17 '12 14:10 diversario

I've turned Kurt Milam's deepExtend mixin into a npm package.

https://github.com/pygy/undescoreDeepExtend/

pygy avatar Dec 07 '12 14:12 pygy