1

In my node.js code, there is a buffer array to which I store the contents of a received image, every time an image being received via TCP connection between the node.js as TCP client and another TCP server:

var byBMP = new Buffer (imageSize);

However the size of image,imageSize, differs every time which makes the size of byBMP change accordingly. That means something like this happen constantly:

 var byBMP = new Buffer (10000);
.... wait to receive another image
 byBMP = new Buffer (30000);
.... wait to receive another image
 byBMP = new Buffer (10000);
... etc

Question:

Is there any more efficient way of resizing the array byBMP. I looked into this: How to empty an array in JavaScript? which gave me some ideas ebout the efficient way of emptying an array, however for resizing it I need you guys' comments.node.js

Community
  • 1
  • 1
C graphics
  • 7,308
  • 19
  • 83
  • 134
  • Do you know if you actually have a problem here? Have you measured to see if this is actually an inefficiency? – Joe Jan 08 '14 at 20:01

1 Answers1

1

The efficient way would be to use Streams rather than a buffer. The buffer will keep the content of the image in your memory which is not very scalable.

Instead, simply pipe the download stream directly to a file output:

imgDownloadStream.pipe( fs.createWriteStream('tmp-img') );

If you're keeping the img in memory to perform transformation, then simply apply the transformation on the stream buffers parts as they pass through.

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134