简体   繁体   English

JavaScript编号,内存大小相同?

[英]JavaScript numbers, all the same size in memory?

I'm reading the Number Type section of the book Professional JavaScript for Web Developers . 我正在阅读专业JavaScript for Web Developers一书中的数字类型部分 It seems to say that all ECMAScript numbers are binary64 floating point, which is corroborated by this MDN article . 似乎所有ECMAScript数都是二进制64浮点数, 这个MDN文章证实了这一点 But the book author also says: 但是这本书的作者也说:

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总是寻找将值转换为整数的方法。

I expected numbers to each occupy the same amount of memory: 64 bits. 我期望每个数字占用相同的内存量:64位。 And the MDN article says, "There is no specific type for integers". MDN文章说,“没有特定的整数类型”。 Anyone know what the book author meant? 有谁知道这本书作者的意思? How can integers take up less memory when they're stored as 64-bit floats (if I have that right)? 当整数存储为64位浮点数时(如果我有权利),整数如何占用更少的内存? You'll find the whole section at the link above (free sample of the book). 您可以在上面的链接中找到整个部分(本书的免费样本)。

JavaScript doesn't have any other number type than double precision floating point (except in ECMAScript 6 typed arrays ), but the underlying implementation may choose to store the numbers in any way it likes as long as the JavaScript code behaves the same. JavaScript没有任何其他数字类型而不是双精度浮点(ECMAScript 6 类型数组除外),但只要JavaScript代码的行为相同,底层实现可以选择以任何方式存储数字。

JavaScript is compiled nowadays, which means that it can be optimised in many ways that are not obvious in the language. 现在编译JavaScript,这意味着它可以通过语言中不明显的许多方式进行优化。

If a local variable in a function only ever takes on an integer value and isn't exposed outside the function in any way, then it could actually be implemented using an integer type when the code is compiled. 如果函数中的局部变量只占用整数值并且没有以任何方式暴露在函数外部,那么在编译代码时它实际上可以使用整数类型实现。

The implementation varies in different browsers. 实现因浏览器而异。 Currently it seems to make a huge difference in MS Edge, a big difference in Firefox, and no difference at all in Chrome: http://jsperf.com/int-vs-double-implementation (Note: jsperf thinks that MS Edge is Chrome 42.) 目前它似乎在MS Edge中产生了巨大的差异,这是Firefox中的一个巨大差异,在Chrome中没有任何区别: http ://jsperf.com/int-vs-double-implementation(注意:jsperf认为MS Edge是Chrome 42.)


Further research: 进一步的研究:

The JS engines Spidermonkey (Firefox), V8 (Chrome, Opera), JavaScriptCore (Safari), Chakra (IE) and Rhino (and possibly others, but those are harder to find implementation details about) use different ways of using integer types or storing numbers as integers when possible. JS引擎Spidermonkey(Firefox),V8(Chrome,Opera),JavaScriptCore(Safari),Chakra(IE)和Rhino(可能还有其他人,但是那些更难找到实现细节)使用不同的方式使用整数类型或存储尽可能将数字作为整数。 Some quotes: 一些引言:

"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." “为了有效地表示数字和JavaScript对象,V8代表我们两个都有32位值。它使用一个位来知道它是一个对象(flag = 1)还是一个整数(flag = 0)在这里调用SMall整数或SMI,因为它的31位。“

http://thibaultlaurens.github.io/javascript/2013/04/29/how-the-v8-engine-works/ 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." “JavaScript没有内置的整数值概念,但为了提高效率,JavaScriptCore将大多数整数表示为int32而不是double。”

http://trac.webkit.org/wiki/JavaScriptCore 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." “[...]非double值是32位类型标记和32位有效负载,通常是指针或带符号的32位整数。”

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals 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." “在Windows 10和Microsoft Edge中,我们已经开始优化Chakra的解析器和JIT编译器,以识别全局定义的整数的非常量变量声明,并且在程序执行期间永远不会更改。”

https://blogs.windows.com/msedgedev/2015/05/20/delivering-fast-javascript-performance-in-microsoft-edge/ https://blogs.windows.com/msedgedev/2015/05/20/delivering-fast-javascript-performance-in-microsoft-edge/

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总是寻找将值转换为整数的方法。

This paragraph is complete nonsense. 这段完全是胡说八道。 Ignore it! 忽略它!

Numbers are numbers. 数字是数字。 ECMAScript makes no distinction whatsoever between floating-point and integer numeric values. ECMAScript在浮点数和整数数值之间没有任何区别。

Even within most JS runtimes, all numeric values are stored as double-precision floating point. 即使在大多数JS运行时中,所有数值都存储为双精度浮点。

Not sure I fully understood your question, but "There is no specific type for integers" means that JavaScript doesn't recognize separate types for integers and floats, but they are both typed as Numbers. 我不确定我是否完全理解你的问题,但是"There is no specific type for integers"意味着JavaScript不能识别整数和浮点数的单独类型,但它们都被键入为数字。 The int/float separation happens "behind the curtains", and that's what they meant by "ECMAScript always looks for ways to convert values into integers" . int / float分离发生在“幕后”,这就是他们所说的"ECMAScript always looks for ways to convert values into integers"

The bottom line is that you don't have to worry about it, unless you specifically need your variables to mimic integers or floats for use in other languages, in which case it's probably (did I say probably ?) the best to pass them as strings (because you'd have trouble passing, say, 5.0 as a float because JS would immediatelly convert it to 5, exactly because of the "ECMAScript always looks for ways to convert values into integers" part). 底线是您不必担心它,除非您特别需要您的变量来模仿整数或浮点数以便在其他语言中使用,在这种情况下,它可能(我可能会可能 ?)最好将它们传递给字符串(因为你很难将5.0作为一个浮点数传递,因为JS会立即将它转换为5,正是因为"ECMAScript always looks for ways to convert values into integers"部分)。

 alert(5.0); // don't expect a float from this 

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

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