Test in function cached array creation.

JavaScript performance comparison

Revision 4 of this test case created by Daryl

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    /** caches array creation **/
    var _93Bytes = function(a,b,c,d){b=0;for(c in a)d=a.toLowerCase().charCodeAt(c)-96,b+=d<0^d>26?0:d;return b};
    var _74Bytes = function b(a){return!!a&&Math.max(0,~~parseInt(a[0],36)-9)+b(a.slice(1))};
    var withCache = (function(a,i,s,r) {
        for (; 26 >= i; a[String.fromCharCode(s + i)] = i++);
        return function(w) {
            for (c in (w = w.toLowerCase())) {
                r += (a[w[c]] || 0);
            }
            return r;
        }
    })([],1,96,0);
   
    /** does not cache array creation; creates array everytime **/
   
    var withoutCache = function(w) {
        var a = [],
            i = 1,
            s = 96,
            b = [],
            r = 0;
        for (; 26 >= i; a[String.fromCharCode(s + i)] = i++);
   
        for (c in (w = w.toLowerCase())) {
            r += (a[w[c]] || 0);
        }
        return r;
    };
   
    var withCacheWithWhile = (function(a,s,r,l) {
        while(--l) a[String.fromCharCode(s + l)] = l;
        return function(w) {
            for (c in (w = w.toLowerCase())) {
                r += (a[w[c]] || 0);
            }
            return r;
        }
    })([],96,0,26);
   
    var withCacheWithWhileFinal = (function(e,t,n){while(--n)e[String.fromCharCode(96+n)]=n;return function(n){for(c in n=n.toLowerCase())t+=e[n[c]]||0;return t}})([],0,26);
   
    var withCacheWithForFinal = (function(e,t,n){for(;--n;e[String.fromCharCode(96+n)]=n);return function(r){for(n in r=r.toLowerCase())t+=e[r[n]]||0;return t}})([],0,27);
   
    var withStaticAlphabet = function(e){var t="abcdefghijklmnopqrstuvwxyz",n=0;for(c in e=e.toLowerCase())n+=t.indexOf(e[c])+1||0;return n};
   
    var _113Bytes = function(a){for(b=0,c=a.length;c--;)b+="abcdefghijklmnopqrstuvwxyz".indexOf(a[c].toLowerCase())+1||0;return b};
   
    var _115Bytes = function(a){for(b=0,c=a.length-1;b+='abcdefghijklmnopqrstuvwxyz'.indexOf(a[c].toLowerCase())+1||0,c--;);return b};
   
    var _112Bytes = function(a){for(b=0,c=a.length-1;b+='abcdefghijklmnopqrstuvwxyz'.indexOf(a[c].toLowerCase())+1,c--;);return b};
   
};
</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
cached array creation
withCache('compare');
pending…
not cached array creation
withoutCache('compare');
pending…
cached array creation with while
withCacheWithWhile('compare');
pending…
cached array creation with while final
withCacheWithWhileFinal('compare');
pending…
cached array creation with for final
withCacheWithForFinal('compare');
pending…
with static alphabet
withStaticAlphabet('compare');
pending…
_113Bytes
_113Bytes('compare');
pending…
_115Bytes
_115Bytes('compare');
pending…
_112Bytes
_112Bytes('compare');
pending…
_74Bytes
_74Bytes('compare');
pending…
_93Bytes
_93Bytes("compare");
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment