简体   繁体   English

JavaScript数组如何存储在内存中

[英]How are JavaScript arrays stored in memory

So, I was thinking how arrays are stored in memory in JavaScript. 因此,我在考虑如何在JavaScript中将数组存储在内存中。

I already read How are JavaScript arrays represented in physical memory? 我已经读过JavaScript数组如何在物理内存中表示? , but I couldn't find my answer. ,但找不到答案。

What I'm thinking is more about the memory location of the array units. 我在想的更多是关于阵列单元的内存位置。 In C for example, you need to define the size of the array when you define them. 例如,在C语言中,定义数组时需要定义数组的大小。 With this, C defines a whole block of memory, and it can look the exact location of each unit. 这样,C定义了整个内存块,并且它可以查找每个单元的确切位置。

For example: 例如:

int array[10]; // C knows the memory location of the 1st item of the array

array[3] = 1   // C can do that, because it can calculate the location 
               // of array[3] by doing &array + 3 * (int size)

In JS, you can grow the size of an array after allocating memory to other stuff, which means JS doesn't work with the "block" type of array. 在JS中,可以在将内存分配给其他对象之后增加数组的大小,这意味着JS不适用于“块”类型的数组。

But if arrays are not a single block of memory, how does JS calculate where each unit is? 但是,如果数组不是单个内存块,那么JS如何计算每个单元在哪里? Do JS arrays follows a linked list type of structure? JS数组遵循链接列表类型的结构吗?

Unlike C or other compiled languages that are proprietary, JavaScript is an ECMAScript implementation. 与C或其他专有的编译语言不同,JavaScript是ECMAScript实现。 The details of the implementation are not standardized and are specific to each vendor's implementation. 实施细节未标准化,并且特定于每个供应商的实施。 In short, the low level details of how the language is implemented is a black box and while you can certainly dive into the internals of a particular vendor's implementation, there is no standard on this and implementations will vary from one vendor to another. 简而言之,该语言的实现方式的底层细节是一个黑匣子,尽管您当然可以深入了解特定供应商实施的内部情况,但是对此并没有任何标准,而且各个供应商的实施情况会有所不同。

One thing I would recommend everyone is that node.js recently became a first-class citizen of Chrome V8, so I would recommend studying V8 to see not only how it handles these implementation details but also why. 我向所有人推荐的一件事是,node.js最近成为了Chrome V8的一等公民,因此,我建议研究V8,不仅要了解它如何处理这些实现细节,还应了解原因。

First, This article should prove beneficial to readers because of its focus on writing optimized isomorphic JavaScript: 首先,由于它专注于编写优化的同构JavaScript,因此对读者来说应该是有益的:

https://blog.sessionstack.com/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e https://blog.sessionstack.com/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e

The above article goes into details about how the JIT (Just In Time) compiler works, so you should be able to derive the exact questions you have after reading it. 上面的文章详细介绍了JIT(Just In Time)编译器的工作原理,因此您应该能够在阅读JIT之后得出确切的问题。

Here is an exerpt: 摘录:

Arrays : avoid sparse arrays where keys are not incremental numbers. 数组 :避免键不是增量数字的稀疏数组。 Sparse arrays which don't have every element inside them are a hash table. 在其中没有每个元素的稀疏数组是一个哈希表。 Elements in such arrays are more expensive to access. 这样的数组中的元素访问起来更昂贵。 Also, try to avoid pre-allocating large arrays. 另外,请尝试避免预先分配大数组。 It's better to grow as you go. 随您的成长最好。 Finally, don't delete elements in arrays. 最后,不要删除数组中的元素。 It makes the keys sparse. 它使键稀疏。

Second, I would also recommend reading this and then working outward with respect to V8: http://www.jayconrod.com/posts/52/a-tour-of-v8-object-representation 其次,我也建议阅读此书,然后针对V8着手研究: http : //www.jayconrod.com/posts/52/a-tour-of-v8-object-representation

Third, as a matter of critical bonus facts, I read this answer a while ago and I mentally revisit it from time to time. 第三,关于重要的奖金事实,我前一段时间读了这个答案,我不时重新思考它。 I am extremely surprised I just found it now. 我现在才发现它,我感到非常惊讶。 I literally Googled "stack overflow optimize train tracks" and found it. 我从字面上用Google搜索“堆栈溢出优化火车轨道”并找到了它。 Thanks Google: Why is it faster to process a sorted array than an unsorted array? 感谢Google: 为什么处理排序数组要比未排序数组快?

Yes, that answer does have 27,000 positive votes. 是的,该答案确实有27,000票赞成。

That article talks about branch prediction, and I would like you to be aware of that because it could have some implications on how you work with data in general not just arrays. 那篇文章讨论分支预测,我希望您知道这一点,因为它可能会对您如何处理数据(而不只是数组)产生一些影响。 Again, note the first article I linked, and pay attention while it is describing the order of keys on an Object . 同样,请注意我链接的第一篇文章,并在描述order of keys on an Object要注意。

Performance can be optimized by understanding the implementation details and understanding why the problems were solved that way. 可以通过了解实施细节和了解为什么以这种方式解决问题来优化性能。

Finally , everything is an Object in JavaScript unless it is a scalar value, which we call primitives--String, Number, Boolean, etc. 最后 ,所有东西都是JavaScript中的Object,除非它是一个标量值,我们称它为原语-字符串,数字,布尔值等。

Here is an example for thought provoking purposes: 这是一个发人深省的示例:

const arr = ['one', 'two', 'three']

const sameArr = {
  0: 'one',
  1: 'two',
  2: 'three',
}

We could then destructure our Array as if it were an Object: 然后,我们可以将数组分解为一个对象:

 const yolo = ['one', 'two', 'three'] const { 0: one, 1: two, 2: three, } = yolo console.log('Pretty cool:', one, two, three) 

You can get some hints from that example as to why changing the order of keys could wreak havoc on the underlying hash table. 从该示例中可以得到一些提示,说明为什么更改键的顺序会对基础哈希表造成严重破坏。 Just because you can't see the keys doesn't mean they aren't there and affected. 仅仅因为您看不到按键并不意味着它们不存在且不受影响。

In the above example, if it were a map, you could do sameArr.get('0') and JavaScript would reasonably know exactly where that is in the numerical table. 在上面的示例中,如果它是一个映射,则可以执行sameArr.get('0') ,JavaScript会合理地确切知道它在数值表中的位置。

I would also recommend being careful reading old JavaScript material because of the overhauls of ES6. 由于ES6的大修,我还建议您仔细阅读旧的JavaScript资料。 I feel the most comfortable directing you to V8 material. 将您带到V8材料方面,我感到最舒适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个Javascript的号码是如何存储在memory中的 - How is a Javascript number stored in memory JavaScript中的对象如何存储在内存中? - How are objects in JavaScript stored in memory? memory 如何在 JavaScript 中使用 arrays 进行递归分配 - How is memory allocation for recursion with arrays in JavaScript JavaScript 数组如何在物理内存中表示? - How are JavaScript arrays represented in physical memory? 如何在javascript中点击的位置显示工具提示(内容存储在数组中)? - How to display tooltips(content is stored in arrays) at clicked position in javascript? 如何清除 Javascript 控制台并删除存储在内存中的所有内容? - How do I clear Javascript console and delete everything stored in memory? javascript中可以在内存中存储小数点后多少位数字 - how many digits after decimal point can be stored in memory in javascript WebAssembly &lt;-&gt; JavaScript 内存交互如何与多个类型化数组一起工作? - How does WebAssembly <-> JavaScript memory interaction work with multiple Typed Arrays? 考虑到非类型化的javascript数组不均一,它们如何在内存中布置? - How are untyped javascript arrays laid out in memory considering they're not homogeneous? 是一个存储为javascript内存参考的变量? - is a variable stored as a memory refrence in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM