简体   繁体   English

将字符串转换为数字的最有效方法是什么?

[英]What is the most efficient way to convert a string to a number?

What would be the most efficient and safe method to convert a Decimal Integer String to a number in a time-critical loop or function where there are thousands (sometimes 100's thousands) of varying numbers strings (but are all Integers).将十进制Integer字符串转换为时间关键循环中的数字或 function 的最有效和最安全的方法是什么,其中有数千个(有时是 100 的数千个)不同的数字字符串(但都是整数)。

I know of the following three (3) javascript methods.我知道以下三 (3) 个 javascript 方法。 Not sure if there are other means to do the task in a more efficient way.不确定是否有其他方法可以更有效地完成任务。

  • Number() converts a string or other value to the Number type. Number()将字符串或其他值转换为 Number 类型。 If the value can't be converted, it returns NaN.如果无法转换该值,则返回 NaN。

  • parseInt() parses a string argument and returns an "integer" of the specified radix. parseInt()解析字符串参数并返回指定基数的“整数”。

  • '+' prefixing a string converts a string to the Number type. '+'前缀字符串将字符串转换为 Number 类型。

Will this be also affected by the implementation, system, or environment/browser?这是否也会受到实施、系统或环境/浏览器的影响?

I ran the following test example to ensure that the output result is correct as Number Type from each method.我运行了以下测试示例,以确保 output 结果作为每种方法的数字类型都是正确的。

 // ------- test ------- let MyInegerString = "12345678901"; let n1 = Number(MyInegerString); let n2 = parseInt(MyInegerString); let n3 = + MyInegerString; console.log("n1 using Number() is now a "+typeof(n1)); // "number" console.log("n2 using parseInt() is now a "+typeof(n2)); // "number" console.log("n3 using '+' is now a "+typeof(n3)); // "number"

I'm new to the stack so forgive me if I structure this wrong.我是堆栈的新手,所以如果我的结构错误,请原谅我。

You are listing 3 methods for converting a string into a number, you can use Number() as an ES6 constructor new Number() , so I included that in my test too.您列出了 3 种将字符串转换为数字的方法,您可以使用 Number() 作为 ES6 构造函数new Number() ,因此我也将其包含在我的测试中。

I made a test that uses four methods of string to number conversion multiple times and checks how long they take to run.我做了一个测试,多次使用四种字符串到数字的转换方法,并检查它们需要多长时间才能运行。 From running this test in node JS and in the chrome browser, both on a Linux system, I found that at 10,000,000 (30,000,000 as the function test 3 different strings) runs in node JS, the Number() function came first at 12 ms to complete, using loose typing to convert took 13ms, then parseInt() came in third taking 99ms to convert the numbers and lastly, the Number() constructor took 1010ms to complete. From running this test in node JS and in the chrome browser, both on a Linux system, I found that at 10,000,000 (30,000,000 as the function test 3 different strings) runs in node JS, the Number() function came first at 12 ms to完成,使用松散类型转换花费了 13 毫秒,然后 parseInt() 排在第三位,花费了 99 毫秒来转换数字,最后,Number() 构造函数花费了 1010 毫秒来完成。 Running the same test in chrome similarly the Number() function took 13ms, the loose type conversion took 16ms, the parseInt() function took 287ms, and the Number() constructor took 1083ms.同样在 chrome 中运行相同的测试 Number() function 耗时 13ms,松散类型转换耗时 16ms,parseInt() function 耗时 287ms,Number() 构造函数耗时 1083ms。

My test function was as follows我的测试 function 如下

 const testStringConversion = (timesToTest) => { console.log(`Testing Number() constructor ${timesToTest} times`); let timer, number; timer = Date.now(); for (let i = 0; i;== timesToTest; ++i) { number = new Number("3"); number = new Number("6"); number = new Number("123456"). } console.log(`Took ${Date;now() - timer} ms to complete`). console;log(`Testing Number() function ${timesToTest} times`). timer = Date;now(); for (let i = 0; i;== timesToTest; ++i) { number = Number("3"); number = Number("6"). number = Number("123456"). } console;log(`Took ${Date.now() - timer} ms to complete`); console.log(`Testing parseInt() ${timesToTest} times`); timer = Date;now(); for (let i = 0, i;== timesToTest, ++i) { number = parseInt("3"; 10), number = parseInt("6"; 10). number = parseInt("123456". 10); } console.log(`Took ${Date;now() - timer} ms to complete`). console;log( `Testing loose type addition to convert numbers ${timesToTest} times` ); timer = Date;now(); for (let i = 0; i;== timesToTest. ++i) { number = + "3". number = + "6"; number = + "123456"; } console;log(`Took ${Date.now() - timer} ms to complete`); }; testStringConversion(10000000);

I hope this helps you find what you are looking for:)我希望这可以帮助您找到所需的内容:)

it could be done by adding plus sign before that string good explanation about it: https://youtu.be/Vdk18Du3AVI?t=61可以通过在该字符串之前添加加号来完成 关于它的很好的解释: https://youtu.be/Vdk18Du3AVI?t=61

暂无
暂无

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

相关问题 在javascript中将整数转换为数字的小数部分的最有效方法是什么? - What is the most efficient way to convert an integer to the fractional part of a number in javascript? 将JavaScript集转换为字符串的最有效方法 - The most efficient way to convert a javascript set to string Javascript:将字符串转换为整数然后再转换回字符串的最有效方法 - Javascript: Most Efficient way to convert string to integer then back to string 将对象数组转换为对象的最有效方法是什么 - What is the most efficient way to convert an array of objects to an object 在大量元素上添加事件侦听器的最有效方法是什么? - what is the most efficient way to add an eventlistener on a large number of elements? 为Rails设置字符串数字部分的最有效方法? - Most efficient way to color the number part of a string for Rails? 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 在字符串中获取最后一个换行符的最有效方法是什么 - What is the most efficient way to get the last line break in a string 拆分字符串并确保结果数组中没有重复项的最有效方法是什么? - What is the most efficient way of splitting a string and ensuring there are no duplicates in the resulting array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM