• lime!@feddit.nu
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    5 hours ago

    javascript doesn’t have arrays. the backing data structure is a doubly-linked list.

    • qprimed@lemmy.ml
      link
      fedilink
      English
      arrow-up
      6
      ·
      5 hours ago

      and the backing for that is linear or page addressed MOS transistors, spinning rust or flippy-round magnets.

      do you have a source that indicates mainstream JS engines internally uses a list structure for arrays? I can’t find one.

      • lime!@feddit.nu
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        3 hours ago

        skipped a few steps there i think.

        anyway, good question. led me to some cursed code.

        the ArrayObject in spidermonkey is an interface to either a TypedArrayObject or a SharedArrayObject. those both have an inner ArrayBuffer object, which is a view into ArrayBufferObjectMaybeShared, which contains a refcounted vector of uint8 pointers, regardless of the datatype. soooo all arrays in javascript are… strings?

        • qprimed@lemmy.ml
          link
          fedilink
          English
          arrow-up
          4
          ·
          3 hours ago

          skipped a few steps there i think.

          thanks for the considered reply. didn’t mean to jump all the way down to electrons and sound so flippant.

          my claim is that JavaScript arrays are arrays because the spec defines their behavior as such. the implementation details are absolutely interesting from a performance perspective and I was genuinely curious how an internally linked list implementation would actually work, real-world. regardless… almost every interaction I have ever had with a JS programmer has ended in “its strings all the way down”… so… I mean… yes-ish?

          loved your poking of the hornets nest in this thread :-)

          • lime!@feddit.nu
            link
            fedilink
            arrow-up
            2
            ·
            2 hours ago

            i was thinking between the linked list and the transistors :)

            also, i mean… what you might call an array i might call a vector. js arrays allow elements of different types, so they are by definition not arrays in the traditional sense. them being chars internally does make sense in a gross way.