JavaScript 个数字,在内存中的大小都一样吗?
JavaScript numbers, all the same size in memory?
我正在阅读 the Number Type section of the book Professional JavaScript for Web Developers. It seems to say that all ECMAScript numbers are binary64 floating point, which is corroborated by this MDN article。但是书作者也说:
Because storing floating-point values uses twice as much memory as storing integer values, ECMAScript always looks for ways to convert values into integers.
我希望每个数字占用相同数量的内存:64 位。 MDN 文章说,"There is no specific type for integers"。有谁知道书作者的意思吗?当整数存储为 64 位浮点数时,整数如何占用更少的内存(如果我说得对的话)?您可以在上面的 link 中找到整个部分(本书的免费样本)。
不确定我是否完全理解您的问题,但 "There is no specific type for integers"
意味着 JavaScript 无法识别整数和浮点数的不同类型,但它们都被键入为数字。 int/float 分离发生在 "behind the curtains",这就是他们所说的 "ECMAScript always looks for ways to convert values into integers"
。
最重要的是你不必担心它,除非你特别需要你的变量来模拟整数或浮点数以用于其他语言,在这种情况下可能是(我说过 可能?) 最好将它们作为字符串传递(因为你很难将 5.0 作为浮点数传递,因为 JS 会立即将其转换为 5,正是因为 "ECMAScript always looks for ways to convert values into integers"
部分).
alert(5.0); // don't expect a float from this
Because storing floating-point values uses twice as much memory as storing integer values, ECMAScript always looks for ways to convert values into integers.
这一段完全是胡说八道。忽略它!
数字就是数字。 ECMAScript 对浮点数和整数数值没有任何区别。
即使在大多数 JS 运行时,所有数值都存储为双精度浮点数。
JavaScript 除了双精度浮点数(ECMAScript 6 typed arrays 除外)没有任何其他数字类型,但底层实现可以选择以任何它喜欢的方式存储数字只要 JavaScript 代码的行为相同。
JavaScript现在是编译的,这意味着它可以在许多语言中不明显的方面进行优化。
如果函数中的局部变量只采用整数值并且不会以任何方式暴露在函数外部,那么它实际上可以在编译代码时使用整数类型来实现。
不同浏览器的实现方式不同。目前看来在 MS Edge 上差别很大,在 Firefox 上差别很大,在 Chrome 里完全没有差别: http://jsperf.com/int-vs-double-implementation (注:jsperf 认为 MS Edge 是 Chrome 42 .)
进一步研究:
JS 引擎 Spidermonkey (Firefox)、V8 (Chrome、Opera)、JavaScriptCore (Safari)、Chakra (IE) 和 Rhino(可能还有其他引擎,但这些更难实现)查找有关的实现细节)尽可能使用不同的方式使用整数类型或将数字存储为整数。一些引用:
"To have an efficient representation of numbers and JavaScript
objects, V8 represents both of us with a 32 bits value. It uses a bit
to know if it is an object (flag = 1) or an integer (flag = 0) called
here SMall Integer or SMI because of its 31 bits."
http://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/
"JavaScript does not have a built-in notion of an integer value, but
for efficiency JavaScriptCore will represent most integers as int32
rather than as double."
http://trac.webkit.org/wiki/JavaScriptCore
"[...] non-double values are a 32-bit type tag and a 32-bit payload,
which is normally either a pointer or a signed 32-bit integer."
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals
"In Windows 10 and Microsoft Edge, we’ve started optimizing Chakra’s
parser and the JIT compiler to identify non const variable
declarations of integers that are defined globally and are never
changed during the course of the execution time of the program."
我正在阅读 the Number Type section of the book Professional JavaScript for Web Developers. It seems to say that all ECMAScript numbers are binary64 floating point, which is corroborated by this MDN article。但是书作者也说:
Because storing floating-point values uses twice as much memory as storing integer values, ECMAScript always looks for ways to convert values into integers.
我希望每个数字占用相同数量的内存:64 位。 MDN 文章说,"There is no specific type for integers"。有谁知道书作者的意思吗?当整数存储为 64 位浮点数时,整数如何占用更少的内存(如果我说得对的话)?您可以在上面的 link 中找到整个部分(本书的免费样本)。
不确定我是否完全理解您的问题,但 "There is no specific type for integers"
意味着 JavaScript 无法识别整数和浮点数的不同类型,但它们都被键入为数字。 int/float 分离发生在 "behind the curtains",这就是他们所说的 "ECMAScript always looks for ways to convert values into integers"
。
最重要的是你不必担心它,除非你特别需要你的变量来模拟整数或浮点数以用于其他语言,在这种情况下可能是(我说过 可能?) 最好将它们作为字符串传递(因为你很难将 5.0 作为浮点数传递,因为 JS 会立即将其转换为 5,正是因为 "ECMAScript always looks for ways to convert values into integers"
部分).
alert(5.0); // don't expect a float from this
Because storing floating-point values uses twice as much memory as storing integer values, ECMAScript always looks for ways to convert values into integers.
这一段完全是胡说八道。忽略它!
数字就是数字。 ECMAScript 对浮点数和整数数值没有任何区别。
即使在大多数 JS 运行时,所有数值都存储为双精度浮点数。
JavaScript 除了双精度浮点数(ECMAScript 6 typed arrays 除外)没有任何其他数字类型,但底层实现可以选择以任何它喜欢的方式存储数字只要 JavaScript 代码的行为相同。
JavaScript现在是编译的,这意味着它可以在许多语言中不明显的方面进行优化。
如果函数中的局部变量只采用整数值并且不会以任何方式暴露在函数外部,那么它实际上可以在编译代码时使用整数类型来实现。
不同浏览器的实现方式不同。目前看来在 MS Edge 上差别很大,在 Firefox 上差别很大,在 Chrome 里完全没有差别: http://jsperf.com/int-vs-double-implementation (注:jsperf 认为 MS Edge 是 Chrome 42 .)
进一步研究:
JS 引擎 Spidermonkey (Firefox)、V8 (Chrome、Opera)、JavaScriptCore (Safari)、Chakra (IE) 和 Rhino(可能还有其他引擎,但这些更难实现)查找有关的实现细节)尽可能使用不同的方式使用整数类型或将数字存储为整数。一些引用:
"To have an efficient representation of numbers and JavaScript objects, V8 represents both of us with a 32 bits value. It uses a bit to know if it is an object (flag = 1) or an integer (flag = 0) called here SMall Integer or SMI because of its 31 bits."
http://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/
"JavaScript does not have a built-in notion of an integer value, but for efficiency JavaScriptCore will represent most integers as int32 rather than as double."
http://trac.webkit.org/wiki/JavaScriptCore
"[...] non-double values are a 32-bit type tag and a 32-bit payload, which is normally either a pointer or a signed 32-bit integer."
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals
"In Windows 10 and Microsoft Edge, we’ve started optimizing Chakra’s parser and the JIT compiler to identify non const variable declarations of integers that are defined globally and are never changed during the course of the execution time of the program."