43

I am struggling with deep copies of objects in nodeJS. my own extend is crap. underscore's extend is flat. there are rather simple extend variants here on stackexchange, but none are even close to jQuery.extend(true, {}, obj, obj, obj) .. (most are actually terrible and screw up the benefits of asnyc code.)

hence, my question: is there a good deep copy for NodeJS? Has anybody ported jQuery's ?

6
  • Avoid doing so. Deep copies are bad. Favour shallow copies.
    – Raynos
    Feb 22, 2012 at 18:02
  • 3
    could you explain why? for me, the shallow copies are a nightmare when they flow into a series of async callbacks...
    – itsatony
    Feb 22, 2012 at 19:12
  • 1
    also - our DB structure (mongoDB) has pretty deep objects and i really do not want to mess around and convert structures... it's very convenient to just work with the very same objects in code & db ...
    – itsatony
    Feb 22, 2012 at 19:16
  • Sure it is. Just don't deep copy them. I work with objects from mongo and I never deep copy them :\
    – Raynos
    Feb 22, 2012 at 19:31
  • 4
    itsatony I disagree with Raynos here, you should use your judgement as to whether this behaviour is right for your use case. Just be aware that there are pitfalls and use your head. This is a debate on the deep copy/extend issue for the Underscore project: github.com/documentcloud/underscore/issues/162 Feb 27, 2012 at 11:08

11 Answers 11

28

It's already been ported. node-extend

Note the project doesn't have tests and doesn't have much popularity, so use at your own risk.

As mentioned you probably don't need deep copies. Try to change your data structures so you only need shallow copies.

Few months later

I wrote a smaller module instead, recommend you use xtend. It's not got an implementation containing jQuery baggage nor does it have bugs like node-extend does.

12
  • 4
    Sorry, how can you say that just because you have never used a deep copy that they're bad and should be avoided in all cases?
    – Josh
    Feb 22, 2012 at 19:44
  • 3
    @itsatony xtend only does shallow extension by design
    – Raynos
    Aug 2, 2012 at 23:09
  • 5
    after trying out a few modules I choose for node.extend because it clones objects using prototypes correctly. xtend and node-extend (with a '-') both fail to do so.
    – donnut
    Sep 9, 2012 at 14:07
  • 3
    @Raynos you should tell you are the author of the library you promote.
    – benweet
    Feb 7, 2014 at 22:30
  • 2
    Even if you are 100% impartial you have to "disclose your affiliation" as stated here
    – benweet
    Feb 8, 2014 at 1:09
15

You want jQuery's, so just use it:

function extend() {
    var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {},
        i = 1,
        length = arguments.length,
        deep = false,
        toString = Object.prototype.toString,
        hasOwn = Object.prototype.hasOwnProperty,
        push = Array.prototype.push,
        slice = Array.prototype.slice,
        trim = String.prototype.trim,
        indexOf = Array.prototype.indexOf,
        class2type = {
          "[object Boolean]": "boolean",
          "[object Number]": "number",
          "[object String]": "string",
          "[object Function]": "function",
          "[object Array]": "array",
          "[object Date]": "date",
          "[object RegExp]": "regexp",
          "[object Object]": "object"
        },
        jQuery = {
          isFunction: function (obj) {
            return jQuery.type(obj) === "function"
          },
          isArray: Array.isArray ||
          function (obj) {
            return jQuery.type(obj) === "array"
          },
          isWindow: function (obj) {
            return obj != null && obj == obj.window
          },
          isNumeric: function (obj) {
            return !isNaN(parseFloat(obj)) && isFinite(obj)
          },
          type: function (obj) {
            return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"
          },
          isPlainObject: function (obj) {
            if (!obj || jQuery.type(obj) !== "object" || obj.nodeType) {
              return false
            }
            try {
              if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
                return false
              }
            } catch (e) {
              return false
            }
            var key;
            for (key in obj) {}
            return key === undefined || hasOwn.call(obj, key)
          }
        };
      if (typeof target === "boolean") {
        deep = target;
        target = arguments[1] || {};
        i = 2;
      }
      if (typeof target !== "object" && !jQuery.isFunction(target)) {
        target = {}
      }
      if (length === i) {
        target = this;
        --i;
      }
      for (i; i < length; i++) {
        if ((options = arguments[i]) != null) {
          for (name in options) {
            src = target[name];
            copy = options[name];
            if (target === copy) {
              continue
            }
            if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
              if (copyIsArray) {
                copyIsArray = false;
                clone = src && jQuery.isArray(src) ? src : []
              } else {
                clone = src && jQuery.isPlainObject(src) ? src : {};
              }
              // WARNING: RECURSION
              target[name] = extend(deep, clone, copy);
            } else if (copy !== undefined) {
              target[name] = copy;
            }
          }
        }
      }
      return target;
    }

and a small test to show that it does deep copies

extend(true, 
    {
        "name": "value"
    }, 
    {
        "object": "value",
        "other": "thing",
        "inception": {
            "deeper": "deeper",
            "inception": {
                "deeper": "deeper",
                "inception": {
                    "deeper": "deeper"
                }
            }
        }
    }
)

But remember to provide attribution: https://github.com/jquery/jquery/blob/master/src/core.js

3
  • Please note that I did not bring over "isPlainObject", "isArray" or any other jQuery files, because I wanted to point out that you can directly capture their source and just use that.
    – jcolebrand
    Feb 22, 2012 at 17:30
  • cool, thanks a ton! I had tried getting it over myself, but i must have messed it up. yours works, mine didn't :(
    – itsatony
    Feb 22, 2012 at 19:14
  • Works like charm! Couldn't use jQuery in Google Appscript, and this helped me a lot!! Nov 30, 2019 at 9:55
11

A quick and dirty answer to deep copies is just to cheat with a little JSON. It's not the most performant, but it does do the job extremely well.

function clone(a) {
   return JSON.parse(JSON.stringify(a));
}
5
  • 3
    That's great if it's just a data oriented object but you wouldn't want to do that if your object came from a particular constructor with it's own methods and inheritance as that would all be lost.
    – marksyzm
    Feb 25, 2013 at 10:44
  • 2
    @marksyzm that's absolutely true; it's only useful for copying simple objects of values; it fails for dates, functions, and in some instances constructed objects.
    – Kato
    Feb 25, 2013 at 14:40
  • unfortunately functions are lost. This works perfecty for everything but functions Dec 30, 2015 at 21:40
  • 2
    No, it doesn't work perfectly for everything but functions. To quote the comment directly before yours: it's only useful for copying simple objects of values; it fails for dates, functions, and in some instances constructed objects.
    – Kato
    Dec 31, 2015 at 2:32
  • Cloning is not necessarily extending. Extending requires a target. Apr 25, 2017 at 16:11
11

Please use the built-in util module:

var extend = require('util')._extend;

var merged = extend(obj1, obj2);
3
  • 12
    This isn't a documented method, and is prefixed with an underscore which usually means it is not intended for public consumption. Mar 24, 2015 at 20:30
  • 3
    Also util._extend is not deep.
    – dbkaplun
    Nov 4, 2015 at 7:29
  • @CraigYounkins That's why privacy conventions don't work in the real world ;) Apr 25, 2017 at 16:09
8

I know this is an old question, but I'd just like to throw lodash's merge into the mix as a good solution. I'd recommend lodash for utility functions in general :)

2
  • I like lodash, but lodash's extend mutates the object and this sucks big time.
    – Wtower
    Dec 12, 2016 at 0:02
  • Lodash's mutation can easily be avoided with an empty object as the first parameter for both merge and extend. var obj3 = lodash.extend(obj1, obj2) will mutate obj1 var obj3 = lodash.extend({}, obj1, obj2) will not mutate obj1. Feb 8, 2017 at 20:37
1

This works for deep object extension... be warned that it replaces arrays rather than their values but that can obviously be updated how you like. It should maintain enumeration capabilities and all the other stuff you probably want it to do

function extend(dest, from) {
    var props = Object.getOwnPropertyNames(from), destination;

    props.forEach(function (name) {
        if (typeof from[name] === 'object') {
            if (typeof dest[name] !== 'object') {
                dest[name] = {}
            }
            extend(dest[name],from[name]);
        } else {
            destination = Object.getOwnPropertyDescriptor(from, name);
            Object.defineProperty(dest, name, destination);
        }
    });
}
1

In Node.js, You can use Extendify to create an _.extend function that supports nested objects extension (deep extend) and is also immutable to it's params (hence deep clone).

_.extend = extendify({
    inPlace: false,
    isDeep: true
});
1

node.extend does it deep and has familiar jQuery syntax

1

just install extend. docs: node extend package install:

npm install extend

then enjoy it:

extend ( [deep], target, object1, [objectN] )

deep is optional. default is false. if switch to true it will recursively merge your objects.

0
0

Sharped version called whet.extend.

I re-write node-extend with CoffeeScript and add travis-ci test suite, because I need deep coping in Node for myself, so now it is here.

And yes, I think in some case its absolutely correctly to use deep merge, for example I use it at config works, when we are need to merge default and user branches together.

0

You also can use my version of extend plugin https://github.com/maxmara/dextend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.