• Where can I find an explanation of how Arrays, typed arrays, and ArrayBuffers behave when you do things like:

    var array2=array1
    

    I know that sometimes it creates a copy, and other times something called a "view", which I don't understand.

    How does this work? Right now, I've been explicitly doing:

    var array2=new Uint8Array(array1); //or whatever type of array it is
    

    every time I need to do anything like this, because this is the only way that I understand the behavior of.

    Where can I get more information on this? AFAIK, this isn't standard javascript (I certainly wasn't able to find anything about typed arrays and array views)... So yeah, I'm kinda baffled, and think it's likely that I could write better code if I understood how this worked, but I can't find any explanations of what these are, and how they work.

    I think I also don't understand this on a conceptual level - I don't comprehend why these "views" and why things-that-look-like-copies-and-are-cre­ated-like-copies-but-aren't-copies* are a good thing. Why define a new variable if you're not copying the data? What is the benefit? I already have the array, why not just refer to it instead of creating... what amounts to an alias to it.

    My initial reactions when running into it was profanity-laden exclamations accusing the unknown language-designer who made this system of mental deficiencies, satanic inspiration, despicable perversions and general malice toward programmers. But yeah, I think I must be missing something here. Be aware that while I work on programming at work and in my free time, I didn't major in CS (advanced degree in the wrong technical major - one much harder to get through, but which I could never deal with the work culture in. Biggest mistake of my life) - I think that if I did, I might have learned about these in school.

    *that is, it fails the duck test - it hatches from a duck egg, looks like a duck, quacks like a duck, but when you pluck it and roast it up, it's mom back in the pond gets cooked up too.

  • I'm not at a PC at the moment so I can't comment properly, but this is all totally standard js. In js doing a simple assignment won't copy, using a view and initialising with another will copy, but initialising with view.buffer will re-use the same data.

    The views are a convenient way to access data in different ways - for example as uint16

    Bit more info here: https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Typed_arrays

  • Wow. I never knew about this. In years of working with javascript, I've never encountered views or typed arrays. I think I default to the wrong resources for javascript - Since I come at this from web design, I'm use to using resources focused on that, and this stuff, well, doesn't really come up in web design.

    The whole thing makes a lot more sense having read that, and I see what the point of "views" are; I hadn't realized that there was that distinction between the view and the data in typed arrays. That's really powerful...

    (also - I hope what I said above didn't come off as abrasive - I was trying to be silly, and recognize that I knew I was doing something wrong)

    Does the Espruino have StringView? That would be super useful - I'm always turning Uint8Arrays into strings (so that I can concatenate them, and then write them to some device. I have to convert them to strings since I need to be able to prefix the data with a few bytes to tell the device what this blob of bytes is for, and I can't fit a simple array large enough in memory)

  • You didn't come across as abrasive at all - JavaScript has caused a few profanity-laden exclamations from me too :)

    The only frustrating thing about typed arrays is:

    var a = new Uint8Array(10);
    var b = new Uint8Array(a); // data copied
    var c = new Uint8Array(a.buffer);  // data not copied
    

    StringView would make a lot of sense actually - or some way to get strings from a uint8array. I've actually added E.toArrayBuffer for strings so the simplest way might be to add the reverse function too though.

    In terms of adding to ArrayBuffers, what I tend to do is:

    var source = new Uint8Array(...);
    var dest = new Uint8Array(source.length+3);
    dest.set([1,2,3]);
    dest.set(source, 3);
    

    Having said that, I wonder whether SPI.send/I2C.read/etc would benefit from being able to take an arbitrary number of arguments. It might make the code simpler in a lot of cases.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Arrays, Copying and Views. Can someone explain this to me?

Posted by Avatar for DrAzzy @DrAzzy

Actions