Mar*_*ark 313 javascript performance
如何parseInt()以及Number()将字符串转换为数字时不同的表现?
CMS*_*CMS 433
好吧,它们在语义上是不同的,被Number称为函数的构造函数执行类型转换并parseInt执行解析,例如:
// parsing:
parseInt("20px"); // 20
parseInt("10100", 2); // 20
parseInt("2e1"); // 2
// type conversion
Number("20px"); // NaN
Number("2e1"); // 20, exponential notation
Run Code Online (Sandbox Code Playgroud)
请记住,如果parseInt检测到字符串上的前导零,它将解析八进制数中的数字,这在标准的新版本ECMAScript 5上已经改变,但是它需要很长时间才能进入浏览器实现(它是与ECMAScript 3)不兼容,parseInt也会忽略与当前使用的基数的任何数字不对应的尾随字符.
该Number构造函数不检测八进制:
Number("010"); // 10
parseInt("010"); // 8, implicit octal
parseInt("010", 10); // 10, decimal radix used
Run Code Online (Sandbox Code Playgroud)
但它可以用十六进制表示法处理数字,就像parseInt:
Number("0xF"); // 15
parseInt("0xF"); //15
Run Code Online (Sandbox Code Playgroud)
另外,一个广泛使用的构造来执行数值类型转换,是一元+运算符(第72页),它相当于使用Number构造函数作为函数:
+"2e1"; // 20
+"0xF"; // 15
+"010"; // 10
Run Code Online (Sandbox Code Playgroud)
let*_*nje 21
typeof parseInt("123") => number
typeof Number("123") => number
typeof new Number("123") => object (Number primitive wrapper object)
Run Code Online (Sandbox Code Playgroud)
前两个将为您提供更好的性能,因为它返回原语而不是对象.
Sau*_*ius 15
如果您正在寻找性能,那么可能是最好的结果,您可以通过按位右移"10">>0.也乘以("10" * 1)或不乘(~~"10").所有这些都更快的Number和parseInt.他们甚至有"功能"返回0表示没有数字参数.这是性能测试.
我发现的性能两个环节转换的几种方法中比较string来int.
parseInt(str,10)
parseFloat(str)
str << 0
+str
str*1
str-0
Number(str)
Run Code Online (Sandbox Code Playgroud)
http://phrogz.net/js/string_to_number.html
除非您需要十六进制或八进制,否则最好远离 parseInt 并使用 Number 和 Math.round。两者都可以使用字符串。为什么要远离它?
parseInt(0.001, 10)
0
parseInt(-0.0000000001, 10)
-1
parseInt(0.0000000001, 10)
1
parseInt(4000000000000000000000, 10)
4
Run Code Online (Sandbox Code Playgroud)
它完全屠杀了非常大或非常小的数量。奇怪的是,如果这些输入是字符串,它就能正常工作。
parseInt("-0.0000000001", 10)
0
parseInt("0.0000000001", 10)
0
parseInt("4000000000000000000000", 10)
4e+21
Run Code Online (Sandbox Code Playgroud)
我不会冒着很难发现这个错误和人们提到的其他陷阱的风险,我会避免parseInt,除非你需要解析除基数 10 以外的东西Number。Math.round、 、Math.floor、 和.toFixed(0)都可以做同样的事情,parseInt而不需要这些类型的错误。
如果您确实想要或需要使用 parseInt 来实现它的某些其他特性,请不要使用它来将浮点数转换为整数。
小智 7
parseInt() -> 将数字解析为指定的 redix。
Number()-> 如果失败,则将指定的值转换为其等效的数值或 NaN。
因此,要将一些非数字值转换为数字,我们应该始终使用 Number() 函数。
例如。
Number("")//0
parseInt("")//NaN
Number("123")//123
parseInt("123")//123
Number("123ac") //NaN,as it is a non numeric string
parsInt("123ac") //123,it parse decimal number outof string
Number(true)//1
parseInt(true) //NaN
Run Code Online (Sandbox Code Playgroud)
parseInt()在进行 redix 转换时,函数有各种极端情况,因此我们应该避免使用 parseInt() 函数来进行强制转换。
现在,要检查天气提供的值是否为数字,我们应该使用本机isNaN()函数
我总是使用parseInt,但要注意将导致它进入八进制模式的前导零.
一个微小的差别是他们转换什么undefined或者null,
Number() Or Number(null) // returns 0
Run Code Online (Sandbox Code Playgroud)
而
parseInt() Or parseInt(null) // returns NaN
Run Code Online (Sandbox Code Playgroud)
parseInt():
NaN。parseInt()函数遇到非数值,它将截断其余的输入字符串,仅解析该部分直到非数值。undefined或0,则JS将假定以下内容:
ES5指定应使用10。但是,并非所有浏览器都支持此功能,因此,如果您的数字可以以0开头,请始终指定基数。Number():
Number()构造可以任何参数输入转换成一个数字。如果Number()构造函数无法将输入转换为数字,NaN则将返回。Number()构造还可以处理十六进制数,他们必须开始0x。console.log(parseInt('0xF', 16)); // 15
// z is no number, it will only evaluate 0xF, therefore 15 is logged
console.log(parseInt('0xFz123', 16));
// because the radix is 10, A is considered a letter not a number (like in Hexadecimal)
// Therefore, A will be cut off the string and 10 is logged
console.log(parseInt('10A', 10)); // 10
// first character isnot a number, therefore parseInt will return NaN
console.log(parseInt('a1213', 10));
console.log('\n');
// start with 0X, therefore Number will interpret it as a hexadecimal value
console.log(Number('0x11'));
// Cannot be converted to a number, NaN will be returned, notice that
// the number constructor will not cut off a non number part like parseInt does
console.log(Number('123A'));
// scientific notation is allowed
console.log(Number('152e-1')); // 15.21Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93264 次 |
| 最近记录: |