# Deep Copy of Arrays and Objects using RED.util.cloneMessage()

**URL:** <https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040>\
**Category:** General\
**Created:** [26 June 2020 19:06 UTC](https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040 "2020-06-26T19:06:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [26 June 2020 19:06 UTC](https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040/1 "2020-06-26T19:06:45Z")

</div>

Having opened my eyes to the need to be very intentional on if I am using a copy of a msg or a reference to the message, I have started using [RED.util.cloneMessage()](https://nodered.org/docs/api/modules/v/0.20.0/@node-red_util_util.html#.cloneMessage) when I need a copy to manipulate that won't maninpulate the original msg.

Since messages are objects, getting a clone of the msg seems like a special case of the more general problem of needing to clone objects/arrays. From what I have found the general case seems to be referred to in the general Javascript world as making a Deep copy. I have done some (minor) experimentation inside a Node-RED function node and found that RED.util.cloneMessage() seems to hand the general case and not just the clone message case. Here is some code that shows it working:

```auto
a = [1,[2,3]]
c = RED.util.cloneMessage(a)
    /*
    Alternate more standard Javascript way of making a deep copy of a 
    c = JSON.parse(JSON.stringify(a))
    */
c[1].push(4)
node.warn(a)
node.warn(c)

```

With these results:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df104e0a2ed6930b28f61c46b4696b57a7e839c9.png)

I have 2 resulting questions for those more knowledgeable than me:

1. Other than the obvious drawback that code using RED.util.cloneMessage() would not inherently work outside of the Node-RED environment, what is the drawback of using it whenever a deep copy (clone) is needed?
2. In the context of Node-RED development are there efficiency differences between using RED.util.cloneMessage() vs JSON.parse(JSON.stringify()) shown in the commented part of my code above?

I also wanted to note, that the JSON.parse(JSON.stringify()) version has some weaknesses that are described [here](https://flaviocopes.com/how-to-clone-javascript-object/) and so might be a bad comparison. That page suggests using Lodash. For my simple case, using a utility library is a little to many extra steps

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 June 2020 20:41 UTC](https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040/2 "2020-06-26T20:41:15Z")

</div>

If you dig into the util.js code inside the Node-RED source you will find that it requires... `const clonedeep = require("lodash.clonedeep");`

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 June 2020 20:51 UTC](https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040/3 "2020-06-26T20:51:32Z")

</div>

As Dave points out, `cloneMessage` uses the `lodash.clonedeep` module under the covers.

The `JSON.parse(JSON.stringify())` method will only work for JSON encodable values - as the article you link to highlights. The fact we use lodash under the covers means we can handle lots of the common JavaScript object types that cannot be serialised to JSON, such as `Date`.

The `cloneMessage` function also has built-in awareness of `msg.req` and `msg.res` - properties sent by the HTTP In node that cannot be otherwise cloned.

---

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [28 June 2020 01:04 UTC](https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040/4 "2020-06-28T01:04:16Z")

</div>

Based on this, then it seems the best pattern would be to use RED.util.cloneMessage() in Node-RED to take advantage of both lodash.clonedeep and the Node-RED specific additional coding under the covers and probably to use lodash.clonedeep elsewhere.

Thanks for the feedback both of you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 July 2020 01:04 UTC](https://discourse.nodered.org/t/deep-copy-of-arrays-and-objects-using-red-util-clonemessage/29040/5 "2020-07-12T01:04:18Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
