数字精度如何影响 JavaScript 中的性能?

How does number precision affect performance in JavaScript, or does it?

在JavaScript中,所有不同类型的数字只有一种类型。使用的数字中的小数位数(精度)是否会影响性能,尤其是在 JavaScript 中?如果可以,怎么做?

如何在 MongoDB 中保存数字:精确的数字比不太精确的数字需要更多 space 吗?

Does the amount of decimals in the numbers used (precision) affect performance especially in JavaScript? If it does, how?

没有。 JavaScript 中的数字类型是以 2 为底的 64 位浮点值,并且始终具有相同的 精度 。计算机一点一点地处理该数据,无论该数据代表对人类来说看起来简单的东西 1.0 还是看似复杂的东西 123423.5645632,都无关紧要。事实上,对于基数为 2 的浮点数,'human' 值与 'hard' 一样,因为 1.1 确实由一个更长的数字表示(例如 1.10000000000000054)。这一切都无关紧要,因为计算机真正在 64 个 1 和 0 上运行。浮点数总是有一些神秘的异常,但这些在实践中通常无关紧要。

How about saving numbers in MongoDB: Do precise numbers take more space than less precise ones?

小数存储为双精度数(64 位),无论是 1.0 还是 1.1221234423。同样,这些数据类型的位数是常数。

整数也是如此,但 MongoDB 支持 32 位和 64 位整数。所以 NumberLong 确实比常规的 32 位整数大,并且和双精度数一样大。

一般不会。当数字不适合 31b signed int 时,可能会对性能产生一些影响。

A tour of V8: object representation解释

According to the spec, all numbers in JavaScript are 64-bit floating point doubles. We frequently work with integers though, so V8 represents numbers with 31-bit signed integers whenever possible (the low bit is always 0; this helps the garbage collector distinguish numbers from pointers). So objects with the fast small integers elements kind only contain this type of number. If we want to store a fractional number or a larger integer or a special value like -0, then we need to upgrade the array to fast doubles. This involves a potentially expensive copy-and-convert operation, but it doesn't happen often in practice. fast doubles objects are still pretty fast because all of the numbers are stored in an unboxed representation. If we want to store any other kind of value, e.g., a string or an object, we must upgrade to a general array of fast elements.