在 JavaScript 中将字符串转换为数字的最快方法是什么?

任何数字都是数字,字符串看起来像数字就是数字,其他的都是 NaN。

'a' => NaN
'1' => 1
1 => 1
149325 次浏览

据我所知,有四种方法可以做到这一点。

Number(x);
parseInt(x, 10);
parseFloat(x);
+x;

通过我做的这个快速测试,它实际上取决于浏览器。

Https://jsben.ch/nnbkm

Implicit在3个浏览器中是最快的,但是它使得代码很难阅读... ... 所以选择你喜欢的代码吧!

将字符串转换为整数的一种快速方法是使用按位或如下所示:

x | 0

虽然这取决于它是如何实现的,在理论上它应该相对快(至少像 +x一样快) ,因为它首先将 x转换为一个数字,然后执行一个非常有效的或。

至少有5种方法可以做到这一点:

如果你只想转换成整数,另一种快速(简短)的方法是 双位不是(即使用两个波浪形字符) :

例如:。

~~x;

参考资料: http://james.padolsey.com/cool-stuff/double-bitwise-not/

到目前为止,我所知道的将字符串转换为数字的5种常见方法都有其不同之处(有更多的按位运算符可以工作,但它们都给出与 ~~相同的结果)。这个 JSFiddle 显示了在调试控制台中可以预期的不同结果: http://jsfiddle.net/TrueBlueAussie/j7x0q0e3/22/

var values = ["123",
undefined,
"not a number",
"123.45",
"1234 error",
"2147483648",
"4999999999"
];


for (var i = 0; i < values.length; i++){
var x = values[i];


console.log(x);
console.log(" Number(x) = " + Number(x));
console.log(" parseInt(x, 10) = " + parseInt(x, 10));
console.log(" parseFloat(x) = " + parseFloat(x));
console.log(" +x = " + +x);
console.log(" ~~x = " + ~~x);
}

调试控制台:

123
Number(x) = 123
parseInt(x, 10) = 123
parseFloat(x) = 123
+x = 123
~~x = 123
undefined
Number(x) = NaN
parseInt(x, 10) = NaN
parseFloat(x) = NaN
+x = NaN
~~x = 0
null
Number(x) = 0
parseInt(x, 10) = NaN
parseFloat(x) = NaN
+x = 0
~~x = 0
"not a number"
Number(x) = NaN
parseInt(x, 10) = NaN
parseFloat(x) = NaN
+x = NaN
~~x = 0
123.45
Number(x) = 123.45
parseInt(x, 10) = 123
parseFloat(x) = 123.45
+x = 123.45
~~x = 123
1234 error
Number(x) = NaN
parseInt(x, 10) = 1234
parseFloat(x) = 1234
+x = NaN
~~x = 0
2147483648
Number(x) = 2147483648
parseInt(x, 10) = 2147483648
parseFloat(x) = 2147483648
+x = 2147483648
~~x = -2147483648
4999999999
Number(x) = 4999999999
parseInt(x, 10) = 4999999999
parseFloat(x) = 4999999999
+x = 4999999999
~~x = 705032703

~~x版本在“更多”情况下会产生一个数字,其他版本通常会产生 undefined,但是输入无效(例如,如果字符串包含非数字字符 之后一个有效的数字,它将返回 0)。

溢出

请注意: ~~可能会出现整数溢出和/或位截断,但不会出现其他转换。虽然输入如此大的值是不寻常的,但您需要注意这一点。示例更新为包含更大的值。

一些 Perf 测试表明,标准的 parseIntparseFloat功能实际上是最快的选项,可能被浏览器高度优化,但这完全取决于您的需求,因为 所有选项是 够快了: http://jsperf.com/best-of-string-to-number-conversion/37

这完全取决于如何配置 perf 测试,因为有些测试显示 parseInt/parseFloat 要慢得多。

我的理论是:

  • 谎言
  • 该死的线条
  • 统计数字
  • JSPerf 结果:)

这可能没有那么快,但是有一个额外的好处,那就是确保你的数字至少是一个特定的值(例如0) ,或者至多是一个特定的值:

Math.max(input, 0);

如果你需要确保一个最小值,通常你会这样做

var number = Number(input);
if (number < 0) number = 0;

Math.max(..., 0)使您免于编写两条语句。

这里有一个简单的方法: 在这个例子中,Var num = Number (str) ;是包含字符串的变量。 您可以测试并查看它是如何打开的: 谷歌 Chrome 开发工具,然后转到 控制台并粘贴以下代码。 阅读评论以便更好地理解转换是如何完成的。

// Here Im creating my variable as a string
var str = "258";




// here im printing the string variable: str
console.log ( str );




// here Im using typeof , this tells me that the variable str is the type: string
console.log ("The variable str is type: " + typeof str);




// here is where the conversion happens
// Number will take the string in the parentesis and transform it to a variable num as type: number
var num = Number(str);
console.log ("The variable num is type: " + typeof num);

在字符串前面加上 +操作符。

console.log(+'a') // NaN
console.log(+'1') // 1
console.log(+1) // 1

我发现 num * 1简单、清晰,适用于整数和浮点数..。

您可以尝试使用 单位,一个测量和数据类型转换库,我们刚刚正式发布!UnitOf 速度非常快,体积很小,而且在转换任何数据类型时都非常有效,不会抛出任何错误或 null/未定义。当转换失败时,将返回您定义的默认值或 UnitOf 的默认值。

//One liner examples
UnitOf.DataType("12.5").toFloat(); //12.5 of type Float is returned. 0 would be returned if conversion failed.
UnitOf.DataType("Not A Num").toInt(10); //10 of type Int is returned as the conversion failed.


//Or as a variable
var unit = UnitOf.DataType("12.5");
unit.toInt(5); //12.5 of type Float is returned. 5 would be returned if the conversion failed.
unit.toFloat(8); // 12 of type Int is returned. 8 would be returned if the conversion failed.

最快的方法是使用 -0:

const num = "12.34" - 0;

将字符串转换为数字的7种方法:

let str = "43.2"

1. Number(str) = > 43.2
2. parseInt(str) = > 43
3. parseFloat(str) = > 43.2
4. +str = > 43
5. str * 1 = > 43.2
6. Math.floor(str) = > 43
7. ~~str = > 43