Fastest number test

Tuesday, 19 August 2008 10:45 by MartinKirk

This is THE fastest way and the most secure to test a string being a number.

I've also tried using Regex which worked ok (~800ms i recall)

var s = ["123e6","not num", "not num"];
P = 100000;
y = 0;
d = new Date();

do{
    if( !isNaN(s[P%3]) )
        y++;
}while(--P);

console.log( (new Date()).getTime() - d.getTime() )

it only takes about 515ms which is very fast on avarage! 

Together with the new int/float regex:  /(([-]|\b)\d+\.?\d*|[-]?\.\d+)(e[-]?\d+|\b)/gi its working really good !!

I've improved the jsParser speed from 6.8ms to 5.3ms !! 

 

Tags:   ,
Categories:   Javascript | MDK-Editor | Benchmarking
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed
Comments are closed