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"
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"
Share
Improve this question
edited Feb 4, 2022 at 8:41
Mohsen Alyafei
asked Jul 3, 2020 at 21:48
Mohsen AlyafeiMohsen Alyafei
5,5973 gold badges36 silver badges54 bronze badges
7
- 2 I'm not sure there is such a thing as a "Decimal Integer String". You mean convert a string to a number? JS doesn't care whether the number is whole or not. – Scott Marcus Commented Jul 3, 2020 at 21:50
- 1 Did you do any benchmarking of listed options to find out? – Yevhen Horbunkov Commented Jul 3, 2020 at 21:51
-
1
If I had to pick, I would think that
Number()
would be the slowest, but it's implementation dependent and all will work virtually at the same speed. – Scott Marcus Commented Jul 3, 2020 at 21:52 - 1 I would be concerned why something so critical is being performed in the users browser. – Taplar Commented Jul 3, 2020 at 21:52
- 1 measure measure measure. – Daniel A. White Commented Jul 3, 2020 at 21:53
1 Answer
Reset to default 5I'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 plete, 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 plete. 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 plete`);
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 plete`);
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 plete`);
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 plete`);
};
testStringConversion(10000000);
I hope this helps you find what you are looking for :)