Are JavaScript arrays actually linked lists?

前端 未结 6 738
眼角桃花
眼角桃花 2020-12-15 04:47

I\'m new to Javascript, and notice that you don\'t need to specify an array\'s size and often see people dynamically creating arrays one element at time. This would be a hug

相关标签:
6条回答
  • 2020-12-15 05:15

    It most likely depends on what JavaScript engine you use.

    Internet Explorer uses a mix of sparse arrays and dense arrays to make that work. Some of the more gory details are explained here: http://blogs.msdn.com/b/jscript/archive/2008/04/08/performance-optimization-of-arrays-part-ii.aspx.

    0 讨论(0)
  • 2020-12-15 05:27

    Javascript arrays are not true arrays like in C/C++ or other languages. Therefore, they aren't as efficient, but they are arguably easier to use and do not throw out of bounds exceptions.

    0 讨论(0)
  • 2020-12-15 05:27

    They are actually more like custom objects that use the properties as indexes. Example:

    var a = { "1": 1, "2": 2};
    a.length = 2;
    for(var i=0;i<a.length;i++)
        console.log(a[i]);
    

    a will behave almost like an array, and you can also call functions from the Array.prototype on it.

    0 讨论(0)
  • 2020-12-15 05:28

    The thing about dynamic languages is, well, that they're dynamic. Just like ArrayList in Java, or arrays in Perl, PHP, and Python, an Array in JavaScript will allocate a certain amount of memory and when it gets to be too big, the language automatically appends to the object. Is it as efficient as C++ or even Java? No (C++ can run circles around even the best implementations of JS), but people aren't building Quake in JS (just yet).

    It is actually better to think of them as HashMaps with some specialized methods too anyway -- after all, this is valid: var a = []; a['cat']='meow';.

    0 讨论(0)
  • 2020-12-15 05:34

    Javascript arrays are typically implemented as hashmaps (just like Javascript objects) with one added feature: there is an attribute length, which is one higher than the highest positive integer that has been used as a key. Nothing stops you from also using strings, floating-point numbers, even negative numbers as keys. Nothing except good sense.

    0 讨论(0)
  • 2020-12-15 05:38

    No.

    What JavaScript arrays are and aren't is determined by the language specification specifically section 15.4. Array is defined in terms of the operations it provides not implementation details of the memory layout of any particular data structure.

    Could Array be implemented on top of a linked list? Yes. This might make certain operations faster such as shift and unshift efficient, but Array also is frequently accessed by index which is not efficient with linked lists.

    It's also possible to get the best of both worlds without linked lists. Continguous memory data structures, such as circular queues have both efficient insertion/removal from the front and efficient random access.

    In practice, most interpreters optimize dense arrays by using a data structure based around a resizable or reallocable array similar to a C++ vector or Java ArrayList.

    0 讨论(0)
提交回复
热议问题