Thursday, 18 May 2017

Buffering a large 2D array with Node

I want to buffer a 2D array. My 2D array consists of floating point numbers e.g:

[
    [ 121093.28125, 38477.87890625, 41.57999801635742],
    [ 13.859999656677246, -16.51999855041504, 51.20000076293945 ],
    ...
]

It can be quite large, up to 1 million in length. I need to send this to a child process in node by buffering the array.

My code is:

// dataAsNumbers is the 2D array thats 10,000 in length
var buf = new Buffer(dataAsNumbers);

child.stdin.write(buf);

And in my child process:

process.stdin.on('data', function(buf) {

    console.log('buf.length is ', buf.length);

    var ab = new ArrayBuffer(buf.length);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buf.length; ++i) {
        view[i] = buf[i];
    }

    console.log('ab[0] is ', ab[0]);

    process.exit(0);
});

However the array buffer is undefined:

buf.length is  10000

ab[0] is  undefined

What am I doing wrong?



via Mark

No comments:

Post a Comment