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() 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:
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:
I have 2 resulting questions for those more knowledgeable than me:
- 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?
- 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 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