Node-RED: 暗黙のクローン

# はじめに
Node-REDで大きなデータを扱う際は気を付けないといけません。msgオブジェクトを介してデータを渡す際に、オブジェクト化されて大きくなるだけでなく、いつの間にかmsgオブジェクトと共にクローンが増えてしまうからです。ここでいうクローン(clone)とは、一部の属性をのぞいてほぼオブジェクト全体をdeep copyしたものです。どんな時にクローンが作成されるか、動かして確認しましたので、一覧を作成しました。

# 確認方法
クローンが作成されるかを確認するには以下の方法があります。
## テストフローで確認する
msgごとにタイミングをずらしてデータを更新します。先に更新したmsgの値が別のmsgの更新によって変更されなければclone、更新されていれば参照です。

## ソースコードを確認する
確認したい処理のソースコードを確認します。RED.util.generateId()の呼び出しやDeep copyの処理が行われていればclone(あるいはdeepcopy)、そうでないなら参照あるいはシャローコピー（1階層のみの複製、下位の階層は参照）でしょう。

今回はテストフローで確認しました(v4.0.8を利用)。
![Deepcopy.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/147007/05b60a81-e11f-41ca-a8ac-0127a6846b78.png)


## コード
<div class="highlight"><pre><code>
[{"id":"b2abacfb7b0f5a82","type":"tab","label":"Clone","disabled":false,"info":"","env":[]},{"id":"dd87cf6c850488fd","type":"group","z":"b2abacfb7b0f5a82","name":"複数出力","style":{"label":true,"fill":"#e3f3d3","fill-opacity":"0.28"},"nodes":["30662dc63b67b0c4","82375aadc7fae64d","6c8bd0977f7e4842","92dc576d0995217e","2a706894e5fa9655","0ce6c51d97d7a5b4","8e3ca37c8f216317","6179d53eebdd0afc","54c197dce2158814","bff8a144664f8cd3","565b4e31b15e2280","3ca434d9ca5a554d","eeec512628e72df8","91b84caa18ca65c2","8679dba5d5d0187b","6afed27593afdb9b","92bd23489a98964c","e94a2c2704b03221","80629b65df3373e9","b1a57843db80657a","360b0a9826d14290","c314262042c6454c","e84f50459a6f5193","8c183354fcc70548","83e80021190857fa","dc4497dfe5d2506f","a5c41773b38c4337","e0f0fe01242ee72f","286fe23fa92f59bd","63c0cccbe99af64d","26f135ecfd9bab5f","58b518a3bd74b814","0aac230706395f41","adbe641119e602ee","be1ea486d2a3b84d","9c40f3e52ffb6a58","d2cfaa790cd8898c","97db5e43bec71ab1","209573528a2e8dd8","2abeb8e068395518","f807a6a187320f61","9f1d8330d6c1a05f","5180a20f578ad34c","a63a1d8d98310bef","c0b103a253276515"],"x":14,"y":19,"w":992,"h":742},{"id":"8ec280cf667b8e12","type":"group","z":"b2abacfb7b0f5a82","name":"多重出力","style":{"label":true,"fill":"#bfdbef","fill-opacity":"0.3"},"nodes":["59812a5f2a0ccc83","e5eb403803503e40","406483e16eeebca2","6c64b72220a6ccd7","b501d745e26b54ae","8f4fa9f9db53c47b","7952cd25790ce8eb","1a5aa979e08c897c","378a5aa6da901103","3be61e5bc40a52fb","5f74c322ab4119e0","752674de6d5cdafe","2d67ac9430761387","c1445876d62a2dd9","881ecb50229254cb","164e8bf7778fa4f2","0945a210c9590b21","629bd75c0bf60b1b","62a02cc5274b6d2e","020db1ec72262770"],"x":14,"y":819,"w":972,"h":322},{"id":"30662dc63b67b0c4","type":"inject","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":120,"wires":[["92dc576d0995217e"]]},{"id":"82375aadc7fae64d","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":80,"wires":[]},{"id":"6c8bd0977f7e4842","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":140,"wires":[]},{"id":"92dc576d0995217e","type":"function","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"return msg","func":"\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":120,"wires":[["2a706894e5fa9655","6179d53eebdd0afc"]]},{"id":"2a706894e5fa9655","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":80,"wires":[["0ce6c51d97d7a5b4"]]},{"id":"0ce6c51d97d7a5b4","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":740,"y":80,"wires":[["82375aadc7fae64d"]]},{"id":"8e3ca37c8f216317","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":140,"wires":[["6c8bd0977f7e4842"]]},{"id":"6179d53eebdd0afc","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":140,"wires":[["8e3ca37c8f216317"]]},{"id":"54c197dce2158814","type":"inject","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":400,"wires":[["3ca434d9ca5a554d"]]},{"id":"bff8a144664f8cd3","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 18","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":360,"wires":[]},{"id":"565b4e31b15e2280","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 19","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":420,"wires":[]},{"id":"3ca434d9ca5a554d","type":"function","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"send [msg,msg]","func":"node.send([msg,msg]);\nreturn;","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":400,"wires":[["eeec512628e72df8"],["6afed27593afdb9b"]]},{"id":"eeec512628e72df8","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":360,"wires":[["91b84caa18ca65c2"]]},{"id":"91b84caa18ca65c2","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":740,"y":360,"wires":[["bff8a144664f8cd3"]]},{"id":"8679dba5d5d0187b","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":420,"wires":[["565b4e31b15e2280"]]},{"id":"6afed27593afdb9b","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":420,"wires":[["8679dba5d5d0187b"]]},{"id":"92bd23489a98964c","type":"inject","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":540,"wires":[["b1a57843db80657a"]]},{"id":"e94a2c2704b03221","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 20","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":500,"wires":[]},{"id":"80629b65df3373e9","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 21","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":560,"wires":[]},{"id":"b1a57843db80657a","type":"function","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"send([msg,msg],false)","func":"node.send([msg,msg],false);\nreturn;","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":540,"wires":[["360b0a9826d14290"],["8c183354fcc70548"]]},{"id":"360b0a9826d14290","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":500,"wires":[["c314262042c6454c"]]},{"id":"c314262042c6454c","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":740,"y":500,"wires":[["e94a2c2704b03221"]]},{"id":"e84f50459a6f5193","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":560,"wires":[["80629b65df3373e9"]]},{"id":"8c183354fcc70548","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":560,"wires":[["e84f50459a6f5193"]]},{"id":"83e80021190857fa","type":"inject","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":700,"wires":[["e0f0fe01242ee72f"]]},{"id":"dc4497dfe5d2506f","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 22","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":660,"wires":[]},{"id":"a5c41773b38c4337","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":720,"wires":[]},{"id":"e0f0fe01242ee72f","type":"function","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"send([msg,null],false)","func":"node.send([msg,null],false);\nnode.send([null,msg],false);\nreturn;","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":700,"wires":[["286fe23fa92f59bd"],["58b518a3bd74b814"]]},{"id":"286fe23fa92f59bd","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":660,"wires":[["63c0cccbe99af64d"]]},{"id":"63c0cccbe99af64d","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":740,"y":660,"wires":[["dc4497dfe5d2506f"]]},{"id":"26f135ecfd9bab5f","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":720,"wires":[["a5c41773b38c4337"]]},{"id":"58b518a3bd74b814","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":720,"wires":[["26f135ecfd9bab5f"]]},{"id":"0aac230706395f41","type":"comment","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"Clone","info":"","x":90,"y":60,"wires":[]},{"id":"adbe641119e602ee","type":"comment","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"Clone","info":"","x":90,"y":340,"wires":[]},{"id":"be1ea486d2a3b84d","type":"comment","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"Clone","info":"","x":90,"y":480,"wires":[]},{"id":"9c40f3e52ffb6a58","type":"comment","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"Not clone","info":"","x":100,"y":640,"wires":[]},{"id":"59812a5f2a0ccc83","type":"comment","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"Clone","info":"","x":90,"y":860,"wires":[]},{"id":"e5eb403803503e40","type":"comment","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"Not clone","info":"","x":100,"y":1020,"wires":[]},{"id":"406483e16eeebca2","type":"function","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"payload[0/1], false","func":"let payload = msg.payload\n\nmsg.payload = payload[0];\nnode.send(msg, false);\nmsg.payload = payload[1];\nnode.send(msg, false);\n\n\nreturn;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":1080,"wires":[["1a5aa979e08c897c"]]},{"id":"6c64b72220a6ccd7","type":"change","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":1040,"wires":[["b501d745e26b54ae"]]},{"id":"b501d745e26b54ae","type":"delay","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":720,"y":1040,"wires":[["3be61e5bc40a52fb"]]},{"id":"8f4fa9f9db53c47b","type":"change","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":1100,"wires":[["5f74c322ab4119e0"]]},{"id":"7952cd25790ce8eb","type":"delay","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":1100,"wires":[["8f4fa9f9db53c47b"]]},{"id":"1a5aa979e08c897c","type":"switch","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"=1?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":1080,"wires":[["6c64b72220a6ccd7"],["7952cd25790ce8eb"]]},{"id":"378a5aa6da901103","type":"inject","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"\"no\":1/2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2]","payloadType":"json","x":110,"y":1080,"wires":[["406483e16eeebca2"]]},{"id":"3be61e5bc40a52fb","type":"debug","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":1040,"wires":[]},{"id":"5f74c322ab4119e0","type":"debug","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":1100,"wires":[]},{"id":"752674de6d5cdafe","type":"change","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":880,"wires":[["2d67ac9430761387"]]},{"id":"2d67ac9430761387","type":"delay","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":720,"y":880,"wires":[["629bd75c0bf60b1b"]]},{"id":"c1445876d62a2dd9","type":"change","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":940,"wires":[["62a02cc5274b6d2e"]]},{"id":"881ecb50229254cb","type":"delay","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":940,"wires":[["c1445876d62a2dd9"]]},{"id":"164e8bf7778fa4f2","type":"switch","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"=1?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":920,"wires":[["752674de6d5cdafe"],["881ecb50229254cb"]]},{"id":"0945a210c9590b21","type":"inject","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"\"no\":1/2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2]","payloadType":"json","x":110,"y":920,"wires":[["020db1ec72262770"]]},{"id":"629bd75c0bf60b1b","type":"debug","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"debug 28","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":880,"wires":[]},{"id":"62a02cc5274b6d2e","type":"debug","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":940,"wires":[]},{"id":"020db1ec72262770","type":"split","z":"b2abacfb7b0f5a82","g":"8ec280cf667b8e12","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":290,"y":920,"wires":[["164e8bf7778fa4f2"]]},{"id":"d2cfaa790cd8898c","type":"inject","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":260,"wires":[["2abeb8e068395518"]]},{"id":"97db5e43bec71ab1","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 24","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":220,"wires":[]},{"id":"209573528a2e8dd8","type":"debug","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"debug 25","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":280,"wires":[]},{"id":"2abeb8e068395518","type":"function","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"return [msg,msg]","func":"return [msg,msg];","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":260,"wires":[["f807a6a187320f61"],["a63a1d8d98310bef"]]},{"id":"f807a6a187320f61","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"1st","rules":[{"t":"set","p":"payload","pt":"msg","to":"1st","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":220,"wires":[["9f1d8330d6c1a05f"]]},{"id":"9f1d8330d6c1a05f","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":740,"y":220,"wires":[["97db5e43bec71ab1"]]},{"id":"5180a20f578ad34c","type":"change","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"2nd","rules":[{"t":"set","p":"payload","pt":"msg","to":"2nd","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":280,"wires":[["209573528a2e8dd8"]]},{"id":"a63a1d8d98310bef","type":"delay","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":280,"wires":[["5180a20f578ad34c"]]},{"id":"c0b103a253276515","type":"comment","z":"b2abacfb7b0f5a82","g":"dd87cf6c850488fd","name":"Clone","info":"","x":90,"y":200,"wires":[]}]
</code></pre></div>

# cloneが作成されるパターン
以下のような場合にcloneが作成されていました。
- ノードの出力が複数になる場合（一つのポートから複数のワイヤー）
- ファンクションノードで同じmsgを複数returnする場合
- ファンクションノードで同じmsgを第2引数なしで複数node.sendする場合
- ファンクションノードで同じmsgを第2引数を指定して複数node.sendする場合（trueの場合のみではありませんでした）
- splitノードの出力

# クローンが作成される理由
このように見てくると、クローン作製は、処理フローの分岐や、solitノードで処理が非同期に多重化される場合など、意識せずにブログラムすると危険な場合にクローンが作成されていました。実際にnode.send()などは初期のバージョンでは参照が渡されていましたが、オプションを指定しないとクローンが渡されるようになりました。参照が渡されると別の処理が更新した（想定外の）値で処理してしまうことがあるからだと思います。いわゆる「安全側に倒した設計」が行われたのでしょう。

# cloneを作らない方法
このようによく考えられた設計ではあるのですが、対象のデータがどんどん複製されてしまうとメモリ不足になってしまうこともあります。そこで、以下のような方法を考えると良いでしょう。

- ファンクションノードで同じmsgを1つずつ第2引数にfalseを指定してnode.sendする

出力ポートは一つでも二つでも構いません。一度にmsgを二つ以上送らないことが重要です。

## 注意点

上記の方法は同じmsgを共有することに注意してください。複数個所で更新するとあと勝ちになります。例えば、通常デバッグノードを二股で接続すると分岐したタイミングのデータを出力しますが、上記の方法でfunctionノードから接続すると出力時のデータの状態を出力します。

# おわりに

いかがだったでしょうか？大量データを扱うことはあまりないかもしれません。例えばファイルを読み込んだり、データベースのクエリ結果など、一時データのつもりで読んだのに削除しないままだったり、処理データがたまっていたりすることはないでしょうか？知らぬ間にクローンを作ってしまい、メモリ不足やガベージコレクションが重くならないよう気を付けましょう。
上記でsplitをクローンが作られる一例として紹介しましたが、split-joinで並列処理をする場合は単純なnode.send()ではうまくいきませんので注意してください。