Test in function cached array creation.

JavaScript performance comparison

Test case created by tuccen

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    /** caches array creation **/
    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);
};
</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…

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