C graphics
C graphics

Reputation: 7458

Changing the size of an array in Javascript or node.js efficiently

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

Upvotes: 1

Views: 93

Answers (1)

Simon Boudrias
Simon Boudrias

Reputation: 44609

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.

Upvotes: 1

Related Questions