'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).
I know of the following three (3) javascript methods. 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. If the value can't be converted, it returns NaN.
parseInt() parses a string argument and returns an "integer" of the specified radix.
'+' prefixing a string converts a string to the Number type.
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.
// ------- 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"
Solution 1:[1]
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.
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. 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.
My test function was as follows
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 :)
Solution 2:[2]
it could be done by adding plus sign before that string good explanation about it: https://youtu.be/Vdk18Du3AVI?t=61
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sika Sika |
