JavaScript 数组实际上是链表吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7069250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:51:26  来源:igfitidea点击:

Are JavaScript arrays actually linked lists?

javascriptarrayslinked-list

提问by nw.

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 huge performance problem in other languages as you would constantly need to reallocate memory for the array as it increases in size.

我是 Javascript 的新手,注意到您不需要指定数组的大小,并且经常看到人们一次动态地创建一个元素的数组。在其他语言中,这将是一个巨大的性能问题,因为随着数组大小的增加,您将不断需要为数组重新分配内存。

Is this not a problem in JavaScript? If so, then is there a list structure available?

这在 JavaScript 中不是问题吗?如果是这样,那么是否有可用的列表结构?

采纳答案by vcsjones

It most likely depends on what JavaScript engine you use.

这很可能取决于您使用的 JavaScript 引擎。

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.

Internet Explorer 混合使用稀疏数组和密集数组来完成这项工作。这里解释了一些更血腥的细节:http: //blogs.msdn.com/b/jscript/archive/2008/04/08/performance-optimization-of-arrays-part-ii.aspx

回答by Malvolio

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 alsousing strings, floating-point numbers, even negative numbers as keys. Nothing except good sense.

Javascript 数组通常被实现为哈希映射(就像 Javascript 对象一样),具有一个附加功能:有一个属性length,它比用作键的最大正整数高 1。没有什么阻止你使用字符串,浮点数,甚至是负数作为键。除了理智,别无他法。

回答by cwallenpoole

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).

动态语言的特点是,它们是动态的。就像 Java 中的 ArrayList 或 Perl、PHP 和 Python 中的数组一样,JavaScript 中的数组会分配一定数量的内存,当它变得太大时,语言会自动附加到对象中。它是否与 C++ 甚至 Java 一样高效?不(C++ 甚至可以绕着最好的 JS 实现运行),但是人们还没有在 JS 中构建 Quake(只是还没有)。

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';.

它实际上是更好了反正认为他们与一些专门的方法包含HashMap -毕竟,这是有效的:var a = []; a['cat']='meow';

回答by Mike Samuel

No.

不。

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

JavaScript 数组是什么和不是什么由语言规范特别是第15.4节决定。 Array是根据它提供的操作定义的,而不是任何特定数据结构的内存布局的实现细节。

Could Arraybe implemented on top of a linked list? Yes. This might make certain operations faster such as shiftand unshiftefficient, but Arrayalso is frequently accessed by index which is not efficient with linked lists.

可以Array在链表之上实现吗?是的。这可能使某些操作更快,例如shiftunshift高效,但Array也经常被索引访问,这对链表效率不高。

It's also possible to get the best of both worlds without linked lists. Continguous memory data structures, such as circular queueshave 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++ vectoror Java ArrayList.

在实践中,大多数解释器通过使用基于类似于 C++vector或 Java的可调整大小或可重新分配的数组的数据结构来优化密集数组ArrayList

回答by Eliu

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]);

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

a将表现得几乎像一个数组,你也可以从它的 Array.prototype 调用函数。

回答by FishBasketGordo

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.

Javascript 数组不是像 C/C++ 或其他语言中那样的真正数组。因此,它们效率不高,但可以说更易于使用并且不会抛出越界异常。