tok

JavaScript performance comparison

Test case created by ryanve

Info

similar to PHP strtok()

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function tokA ( s, chars, rtl ) {
        var i = chars.length;
        rtl = true === rtl ? 'pop' : 'shift';
        while ( i-- ) {
            s = s.split(chars[i])[rtl]();
        }
        return s;
    }
   
    function tokB ( s, chars, rtl ) {
        var n, i = chars.length;
        rtl = true === rtl;
        while ( i-- ) {
            n = s.indexOf(chars[i]);
            s = n < 0 ? s : rtl
                ? s.substr(++n)
                : s.substr(0, n);
        }
        return s;
    }
   
    function tokC ( s, chars, rtl ) {
        var n, i = chars.length;
        rtl = true === rtl;
        while ( i-- ) {
            n = s.indexOf(chars[i]);
            s = n < 0 ? s : rtl
                ? s.slice(++n)
                : s.slice(0, n);
        }
        return s;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
tokA
tokA( 'http://example.com?q=value#frag', '?#' );
tokA( 'http://example.com?q=value#frag', '?#', true );
pending…
tokB
tokB( 'http://example.com?q=value#frag', '?#' );
tokB( 'http://example.com?q=value#frag', '?#', true );
pending…
tokC
tokC( 'http://example.com?q=value#frag', '?#' );
tokC( 'http://example.com?q=value#frag', '?#', true );
pending…

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

Compare results of other browsers

0 comments

Add a comment