• There's a small bit of info on http://www.espruino.com/Performance#arra­y-buffers-are-the-most-efficient-way-to-­store-data

    But basically Uint8Array/etc store their data as a string, but they have to have one variable allocated which explains what type (Uint8/Int16/etc) they are, with size and offset. They also have to link to an ArrayBuffer which then references the string, which is mainly so you can do:

    a = new Uint8Array(10)
    b = new Uint16Array(a.buffer)
    a.buffer==b.buffer
    => true
    

    If I just automatically made an ArrayBuffer every time someone referenced .buffer then the two buffers wouldn't be equal.

    You'll say why can't the ArrayBuffer store data directly and the reason is that a String isn't just one datatype - we have string_1,_2,etc depending on the amount of characters in that string block - so we'd have to do the same with ArrayBuffer which would then use up more space in our enum than we have.

    It can help to use trace(..) sometimes to see what a variable is made of:

    >a=new Uint8Array([1,2,3])
    =new Uint8Array([1, 2, 3])
    >E.getSizeOf(a)
    =3
    >trace(a)
    #3677[r1,l1] Uint8Array (offs 0, len 3)  #3676[r1,l0] ArrayBuffer (offs 0, len 3)    #3413[r1,l0] String [1 blocks] "\1\2\3"
    

    So it shows Uint8Array->ArrayBuffer->String

    @fanoush I'm not sure what happened in your test, but a 3 char string should only be one var:

    >E.getSizeOf(E.toString(new Uint8Array([1, 2, 3])))
    =1
    

    So actually the docs are misleading. A String is the most efficient form of storage, but Uint8Array/etc allows you to reference those in a useful way (because normally Strings cannot be modified once created)

  • @fanoush I'm not sure what happened in your test, but a 3 char string should only be one var:

    Don't have newest build but just retried on 52840 dongle with 2.17 and I still see 2, but in Bangle 2 Emulator I see 1 indeed.

    >var a=E.toString(new Uint8Array([1, 2, 3]))
    ="\1\2\3"
    >E.getSizeOf(a)
    =2
    >process.memory()
    ={ free: 13963, usage: 37, total: 14000, history: 10,
      gc: 0, gctime: 20.6298828125, blocksize: 14, stackEndAddress: 537092008, stackFree: 39728,
      flash_start: 0, flash_binary_end: 447188, flash_code_start: 516096, flash_length: 1048576 }
    >process.version
    ="2v17.450"
    >trace(a)
    #24[r1,l1] FlatString [2 blocks] "\1\2\3"
    =undefined
    > 
    

    EDIT: added trace

    EDIT2: maybe that's older E.toString() which still only made flat strings?

About

Avatar for fanoush @fanoush started