• I've made a note to update the Performance/Internals pages, but basically:

    • In Espruino, ArrayBuffers actually store their data in a String behind the scenes
    • Normal Strings are 'chained' - this is mentioned in the performance/internals pages, but basically it means a String can be fragmented all over memory. When you write "Hello" that's what you get, because Espruino didn't know the size of the string up front as it was parsing it as it went along.
    • If Espruino knows the size of the string up front, there is enough contiguous free space, *and the String is long enough that it is more efficient to use a Flat String** then Espruino will allocate a flat string - which is basically a single JsVar as a header, followed by N other JsVars that store the raw data.
    • Flat Strings can take a while to allocate, and because of the fixed JsVar header they're not as efficient for small arrays. There's also no speed advantage even when accessing a small Flat String vs a small string - which is why they're allocated only when required.

    So in your case, your arraybuffer just isn't big enough that it makes sense to allocate it as a flat string. Make it size 32 or something and it'll work.

    You can however use E.toString to make Espruino create a flat string specifically for your data - for example E.toString(new Uint8Array(8)). You could also then use E.toArrayBuffer on that String to create an ArrayBuffer that actually references the Flat String itself.


Avatar for Gordon @Gordon started