1

I am writing a signal processing demo in JavaScript. For that, I need to store relatively large vectors of floats. If I do it in a naive way (using the mathjs library):

var vector = math.matrix([-35.545806884765625, -3.1195175647735596,  ...]);

it works fine, but the source can easily have megabytes.

Is there a way how represent the float e.g., in base64 encoding, so the source code gets compressed?

Jindřich
  • 10,270
  • 2
  • 23
  • 44

1 Answers1

1

One option would be to use typed arrays and serialize underlying byte buffers in hex (two digits per byte). Not as space-efficient as base64, but simple and without all that unicode hassle.

let data = new Float32Array([
    -35.545806884765625,
    -3.1195175647735596
    ]

);

console.log(data)

// encode

let encoded = '';

for (let x of new Uint8Array(data.buffer))
    encoded  += (x | 0x100).toString(16).slice(1);

console.log(encoded)

// decode

let bytes = new Uint8Array(
    encoded
        .match(/../g)
        .map(x => parseInt(x, 16)));

let decodedData = new Float32Array(bytes.buffer)

console.log(decodedData);

If you still want base64, MDN has a couple of suggestions how to do that in the right way: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

georg
  • 211,518
  • 52
  • 313
  • 390