to_num

Benchmark created on


Setup

var string = Math.floor(Math.random() * 10000).toString();

var number = -1;

Test runner

Ready to run.

Testing in
TestOps/sec
With catch
function round(n)
{
    return Math.round(n);
}

function to_number(num)
{
    try{
        num=round(parseInt(num));
        if(num<0) return 0;
        if(!num) num=0;
    }catch(e){num=0};
    return num;
}

for(let i = 0; i < 100000; i++) {
	number = to_number(string);
}

ready
Without catch
function round(n)
{
    return Math.round(n);
}

function to_number(num)
{
    num=round(parseInt(num));
    if(num<0) return 0;
    if(!num) num=0;
    return num;
}

for(let i = 0; i < 100000; i++) {
	number = to_number(string);
}

ready
Without catch + Without round
function round(n)
{
    return Math.round(n);
}

function to_number(num)
{
    num=parseInt(num);
    if(num<0) return 0;
    if(!num) num=0;
    return num;
}

for(let i = 0; i < 100000; i++) {
	number = to_number(string);
}

ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.