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.
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