Saving a Uint8Array to a binary file

These are utilities that I use to download files cross-browser. The nifty thing about this is that you can actually set the download property of a link to the name you want your filename to be. FYI the mimeType for binary is application/octet-stream var downloadBlob, downloadURL; downloadBlob = function(data, fileName, mimeType) { var blob, url; … Read more

Are the advantages of Typed Arrays in JavaScript is that they work the same or similar in C?

Typed Arrays were designed by the WebGL standards committee, for performance reasons. Typically Javascript arrays are generic and can hold objects, other arrays and so on – and the elements are not necessarily sequential in memory, like they would be in C. WebGL requires buffers to be sequential in memory, because that’s how the underlying … Read more

How to append bytes, multi-bytes and buffer to ArrayBuffer in javascript?

You can create a new TypedArray with a new ArrayBuffer, but you can’t change the size of an existing buffer function concatTypedArrays(a, b) { // a, b TypedArray of same type var c = new (a.constructor)(a.length + b.length); c.set(a, 0); c.set(b, a.length); return c; } Now can do var a = new Uint8Array(2), b = … Read more

How do I merge an array of Uint8Arrays?

You can use the set method. Create a new typed array with all the sizes. Example: var arrayOne = new Uint8Array([2,4,8]); var arrayTwo = new Uint8Array([16,32,64]); var mergedArray = new Uint8Array(arrayOne.length + arrayTwo.length); mergedArray.set(arrayOne); mergedArray.set(arrayTwo, arrayOne.length); Alternative: Convert your typed array in “normal” arrays. concat it and create a type array of it again. In … Read more

Javascript Typed Arrays and Endianness

The current behaviour, is determined by the endianness of the underlying hardware. As almost all desktop computers are x86, this means little-endian. Most ARM OSes use little-endian mode (ARM processors are bi-endian and thus can operate in either). The reason why this is somewhat sad is the fact that it means almost nobody will test … Read more

Converting between strings and ArrayBuffers

Update 2016 – five years on there are now new methods in the specs (see support below) to convert between strings and typed arrays using proper encoding. TextEncoder The TextEncoder represents: The TextEncoder interface represents an encoder for a specific method, that is a specific character encoding, like utf-8, iso-8859-2, koi8, cp1261, gbk, … An … Read more