106

When you realize JS Arrays are actually lists instead of actual arrays

Comments
  • 2
    Yup.
  • 19
    And objects are maps
  • 18
    And functions are objects.
  • 11
    I don't follow, what's an actual array then?

    Edit: Nvm a quick Google search solved it πŸ˜„
  • 7
    Switch to .Net, the List<> is an array.
  • 16
    I think many languages cheat a bit with their data structure naming, because for 99% of devs it really doesn't matter how data is stored in memory, as long as it works.

    Just like how sorting algorithms are cute to learn about in college, but in practice you just call data.sort() and the language picks some quicksort.

    Although just like with cars, going manual sometimes gives you a bit of extra performance...
  • 2
    Aren't strings in Java lists? Or arrays? :D

    That picture looks gorgeous tho
  • 2
    True. Came from backend dev.
    Currently doing js for front end dynamicability. Poof. Many things are different
  • 2
    @No-one
    System.Collections.Generic.List<T> contains it's data in an array.
  • 1
    But what are lists and maps made of?
  • 0
    Enlighten me. What would you call it? @No-one
  • 0
    @No-one
    ... You disagreed for that?
  • 1
    @No-one @CWins
    looks like a vector to me

    http://cplusplus.com/reference/...
  • 0
    @No-one
    How do you define a list?

    @vadimir
    Yes!
  • 0
    @No-one
    Okay, so you're not meaning lists as in "linked lists" or these other list-named structures in classic computer science.
  • 1
    @No-one
    i know that lists have a different allocation algorithms than vectors. and i mentioned vectors because its what you meant (is it called meant?) by saying that said data structures acts similarly to an array.

    in terms usefullness vectors always surpass lists in terms of access speed and stability as long as you you dont have to reallocate shit or have the time to do such operations in the background.

    and now to @CWins screenshot
    it looks to me like an array inside the heap which makes the structure technically a vector.

    in the other hand its js and all i know is that its fucking nuts to try to understand how a program written in a scripting language ist structured in terms of memory usage unless you are a developer of said language
  • 1
    My screenshot is c# @vadimir.

    I like c++ vectors. No surprise the c# defaultlist is like that. It's a good combination of simple in technical terms and usability.
  • 0
    @No-one
    i would like to test that but im pretty they dont use more memory. ofc if you want to use them properly you have to use more memory because you want to avoid reallocation in an unpredictable state.

    in terms of access speed it depends highly in the low level possibilities of the cpu. if you are able to jump from one item of a list to the next with the same amount of cycles you need to increase an adress already loaded inside the register. then you are right. on such a cpu lists have no disadvantages in terms of access speed compared to arrays
  • 0
    @No-one
    That's why i mentioned the .net list as an array (though thats not the most precise description). Simple Add/Delete methods are a common thing these days and without knowing the internals, it's easy to waste performance by not setting the capacity before adding 10000 values.
  • 0
    🍺🍻🍺
  • 1
    @No-one actually keep going, I'm enjoying this
  • 1
    @caramelCase
    I will make him work for free.

    @No-one
    Instance is the same as object.
  • 3
    To be fair from a classical definition perspective. The array in JS works like an arraylist. if you analyze it's performance characteristics you get O(1) index look ups. O(1) additions. Slicing and all those operations are easy with lists, but harder with arrays. Also, standard arrays suffer from resizing every now and then. Can you imagine if almost every page had a "stop the world" gotta resize this fucking array issue.
  • 0
    @meowth
    standard arrays dont suffer from resizing because they are static. dont confuse standard arrays with dynamic arrays aka vectors
  • 1
    @vadimir sorry, I meant, the array implementation that most people end up using. I've very rarely used an array that didn't change in size... Maybe if delivered from a server side payload or something.
  • 0
    @No-one for *most* uses, vectors (dynamic arrays) are much more efficient than linked lists. The reason is memory locality.
  • 2
    The discussion somewhat confuses implementation, interface and performance characteristics.

    The two major implementations for a growable sequence are linked list and dynamic array.

    Linked lists do not require contiguous memory, and can guarantee O(1) worst case for appending and prepending an element. Accessing an item in the middle is O(n) non-local memory accesses, which is bad. Also O(n) memory overhead.

    Dynamic arrays (C++ vector, Python lists) have O(1) memory overhead; O(1) access, usually 1-3 machine instructions; appending an element is O(1) amortized, which is good unless you have hard real time requirements; and prepending an element is O(n). Operations are very local in memory so they are very fast on modern hardware.
  • 1
    @elazar if you are looking at the characteristics of a data structure without needing to know it's implementation the only characteristics that matter are its apis and performance. It is why you can safely use objects as hash maps in JS. The performance characteristics and APIs are such that they resemble hashmaps and that's all that matters. It is also why arrays in JS can be treated as stacks, queues, arrays and linked lists.

    I guess I'm saying the performance characteristics describe the data structure equivalently to the API it provides.
  • 0
    @meowth yep. Performance is part of the API.
  • 1
    @bittersweet I often end up implementing linked list like data structures myself, because they are really handy for some applications (especially for chaining stuff like data processing chains, eg. neural network layers).
  • 0
    Who drew that art? It looks like Sayori from DDLC
Add Comment