Fastest string test

Monday, 18 August 2008 19:15 by MartinKirk

because we are now using a parser for the Editor, it's crucial that every small test pr token, is done the outmost optimal way! Any simple small mistake has an impact of 1-2 ms pr parse, which in the end mean that the Editor feel slow and laggy. Undecided

most tokens are taken by the large Switch in jsParser - but un-cased tokens has to be checked manually in the default: - therefore i've just setup a small benchmark of what method is fastest in which 'environment'...

4x charAt()

var P = 100000
var s = "//pingeling"
var y = 0;

var d = new Date();
do{
    if( s.charAt(0)=="/" && s.charAt(1)=="/")
        y++;
    if( s.charAt(0)=="/" && s.charAt(1)=="/")
        y++;
}while(--P)

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

2x substr()

var P = 100000
var y = 0;

d = new Date();
do{
    if( s.substr(0,2)=="//" )
        y++;
    if( s.substr(0,2)=="//" )
        y++;
}while(--P)

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

UPDATE! - 2x indexOf()

var P = 100000
var y = 0;

d = new Date();
do{
    if( s.indexOf("//")==0 )
        y++;
    if( s.indexOf("//")==0 )
        y++;
}while(--P)

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

2x search()

var P = 100000
var y = 0;

d = new Date();
do{
    if( s.search("//" )>=0)
        y++;
    if( s.search("//" )>=0)
        y++;
}while(--P)

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

2x match()

var P = 100000
var y = 0;

d = new Date();
do{
    if( s.match(/^\/\// ))
        y++;
    if( s.match(/^\/\// ))
        y++;
}while(--P)

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

4x mixed(), 1 match

var P = 100000
var y = 0;

var d = new Date();
do{
    if( s.charAt(0)=="*")
        y++;
    if( s.charAt(0)=="?")
        y++;
    if( s.substr(0,2)=="/*" )
        y++;
    if( s.substr(0,2)=="//" )
        y++;
}while(--P)
console.log( (new Date()).getTime() - d.getTime() )

4x mixed(), 1 match, 1 pre-match variable

var P = 100000
var y = 0;

var d = new Date();
do{
    var sub = s.substr(0,2)
    if( s.charAt(0)=="*")
        y++;
    if( s.charAt(0)=="?")
        y++;
    if( sub=="/*" )
        y++;
    if( sub=="//" )
        y++;
}while(--P)
console.log( (new Date()).getTime() - d.getTime() )

The results:

 4x charAt()
 1110 ~ 1125ms
 2x substr()
 750 ~ 797ms
 2x indexOf()
 640 ~ 650ms
 2x search()
 1125 ~ 1172ms
 2x match()  766 ~ 828
 4x mixed(), 1 match  953 ~ 969
 4x mixed(), 1 match, 1 pre-match variable  984 ~ 984

UPDATE hours later
Earlier i conclided that substrings was the fastest... but using indexOf(..)==0 is blazing faster then either of all ! even up to 2x faster...

real world result : a difference from 9ms to 6ms pr render, for the Editor !!  (update: same speed, but in some flooded cases, it will prove better)

I think ill re-make this test...

indexOf is only faster when the test subject always return true... in a mixed environment, i think substring is faster afterall...

Comments

Comments are closed