简体   繁体   English

JS原语是引用吗?

[英]Are JS primitives references?

I have found explanations that primitives and references get stored directly on the stack (static memory - size of the values is known), whereas objects, functions, etc get allocated on the heap (dynamic memory - to be able to grow).我发现原语和引用直接存储在堆栈上的解释(静态 memory - 值的大小已知),而对象、函数等在堆上分配(动态 memory - 能够增长)。

Source: https://felixgerschau.com/javascript-memory-management/来源: https://felixgerschau.com/javascript-memory-management/

Now I've read a few articles where the wording suggests that everything in JS is accessed by reference.现在我读了一些文章,其中的措辞表明 JS 中的所有内容都是通过引用访问的。

https://daveceddia.com/javascript-references/ https://daveceddia.com/javascript-references/

So this would mean that primitives are also stored as reference.所以这意味着原语也被存储为参考。 Is any value stored directly on the stack after all?毕竟有任何值直接存储在堆栈上吗? Another indication is that if you write something like另一个迹象是,如果你写类似

// no prior variable definition
console.log(a);
// ReferenceError: a is not defined

it will actually give you a ReferenceError , although it could be any type (including primitives).它实际上会给你一个ReferenceError ,尽管它可以是任何类型(包括原语)。

So, it seems to me like everything in JS is a reference.所以,在我看来,JS 中的所有内容都是参考。 Is that correct?那是对的吗? If yes - where is a referenced primitive value stored?如果是 -引用的原始值存储在哪里? On the heap?在堆上? On the stack (as it is a primitive)?在堆栈上(因为它是原语)? Can a reference point to the stack?引用可以指向堆栈吗?

I want to share the results of my research in case someone else finds it interesting as well.我想分享我的研究结果,以防其他人也觉得有趣。

While the ECMA specification does not seem to be specific (it is also hard to read from a users point of view), there is plenty of information how V8 (Chromium / NodeJS) handles this matter:虽然 ECMA 规范似乎并不具体(从用户的角度来看也很难阅读),但有很多信息表明 V8(Chromium / NodeJS)如何处理这个问题:

It basically puts everything on the heap and references it with a pointer, except for small integers.它基本上把所有东西都放在堆上并用指针引用它,小整数除外。 Small integers are baked into the pointer with a technique called pointer tagging (encoded in the last bits).使用称为指针标记(在最后一位中编码)的技术将小整数烘焙到指针中。

Here's what the V8 developer blog says about this topic:以下是 V8 开发者博客关于此主题的内容:

https://v8.dev/blog/pointer-compression#value-tagging-in-v8 https://v8.dev/blog/pointer-compression#value-tagging-in-v8

JavaScript values in V8 are represented as objects and allocated on the V8 heap, no matter if they are objects, arrays, numbers or strings. V8 中的 JavaScript 值表示为对象并分配在 V8 堆上,无论它们是对象、arrays、数字还是字符串。 This allows us to represent any value as a pointer to an object.这允许我们将任何值表示为指向 object 的指针。

Many JavaScript programs perform calculations on integer values, such as incrementing an index in a loop.许多 JavaScript 程序对 integer 值执行计算,例如在循环中递增索引。 To avoid us having to allocate a new number object each time an integer is incremented, V8 uses the well-known pointer tagging technique to store additional or alternative data in V8 heap pointers.为避免我们每次递增 integer 时都必须分配一个新数字 object,V8 使用众所周知的指针标记技术在 V8 堆指针中存储附加或替代数据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM