In node.js I have a Buffer (It was stored as a blob in mysql and retrieved using sequelize) I know that this Buffer is an array of 16 bit integers. In the past I've parsed the code with a for loop.
var spectrum_buffer = spectrums[idx]["spectrum"];//this is a buffer
var parsed_spectrum = [];
for (var i = 0; i < spectrum_buffer.length / 2; i++) {
parsed_spectrum[i] = spectrum_buffer.readInt16BE(i * 2);
}
I've read that readInt16BE is slow and that there are typedarrays for arraybuffers now. (which are different than Buffers). Is there a better way of creating an array of ints from this buffer.
Update 1
Based on the feedback I did the following
var arr = new Int16Array(spectrum.buffer)
Which gives me the appropriate type however the bytes are getting swapped. The spectrum buffer is stored in big endian.
< Buffer e1 d7 e0 b9 e3 52 e2 d5 e2 ed e2 92 e2 d6 e2 97 e3 04 e1 95 e1 e2 > e1 d8 e3 14 e2 fd e1 ed e2 d3 e3 09 e1 9f e2 14 e2 f2 e2 54 e2 1f e2 54 > e2 06 e2 8a ... >
The first three numbers are coming across as -10271, -17952, 21219
However they should not vary that much and all three should be negative.
The first number should be -7721 (twos complement always confuses me)
So does Int16Array on node 6 assume big endian or little endian and how do I handle this.