javascript - V8中,null、undefined、boolean等基本类型在内存中是如何存储的?

标签 javascript v8 javascript-engine

boolean 是否在内存中存储为 32 位整数? null 值怎么样?

在书中Speaking Javascript , 它指的是一个类型标签,用于指示存储在内存中的值的类型。例如Object 类型的类型标签是 000。什么是类型标签?

如何找到值类型的 type 标记,例如 booleanstring

最佳答案

来自 Andy Wingo 的 blog post关于主题:

Initially, all JavaScript implementations used tagged pointers to represent JS values. This is a old trick that comes from the observation that allocated memory takes up at least 4 or 8 bytes, and are aligned in such a way that the least significant bit or three will be zero.

所以类型标签允许统一存储所有值。所有值都占用一个机器字(32/64 位),并且根据标签(这是最低有效位),它们被解释为指向对象的指针或一些整数/ bool 值/等,具体取决于标签。

is boolean stored as a 32-byte integer in js memory?

bool 值也占一个字。要获得更具体的答案,我需要查看 v8 源代码。但如果我没记错的话,truefalse 表示为根指针。

how to get the type tag of a value type(boolean,undefined,string, number);

无法通过 JavaScript 执行此操作。它是内部实现细节。

关于javascript - V8中,null、undefined、boolean等基本类型在内存中是如何存储的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55714795/

相关文章:

c++ - v8::ObjectTemplate::SetAccessor 和 v8::Template::Set - 区别

pointers - 为什么 V8 使用指针标记而不是 NaN 装箱?

java - 如何将Object类型从engine.eval转换为int类型

javascript - V8中,null、undefined、boolean等基本类型在内存中是如何存储的?

javascript - 关于 javascript 和 DOM,是否符合标准?

javascript - 如何获取div中包含的字符串

javascript - 使输入建议控件随输入一起移动

javascript - 为什么 Function.prototype.bind 很慢?

c# - 了解 Google V8 的架构

javascript - 是否有可能在木瓜 Dicom 查看器中查看 jpeg 图像?