generated function

JavaScript performance comparison

Test case created by carrilho and last updated

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function generic(func, length){
       
        length = length || func.length;
       
        if(!length) return function(context){
            return func.call(context);
        }
           
        if(length < 0) return function(context){
            return func.apply(context, Array.prototype.slice.call(arguments, 1));
        }
       
        var i = 0, args = ['c'], sargs;
       
        for(; i < length; i++)
            args[i + 1] = 'a' + i;
       
        sargs = args.join(',');
       
        return Function('f', 'return function(' +  sargs + '){return f.call(' + sargs + ');}')(func);
    }
   
    var array = [1, 2, 3,1, 2, 3, 1, 2, 3, 1, 2, 3];
   
    var manualSlice = (function(){
        var nativeSlice = Array.prototype.slice;
        return function(object, a, b){
            return nativeSlice.call(object, a, b);
        }
    })();
   
    var slice = generic(Array.prototype.slice);
   
   
    var manualToString = (function(){
        var nativeToString = Object.prototype.toString;
        return function(object){
            return nativeToString.call(object);
        }
    })();
   
    var toString = generic(Object.prototype.toString);
   
};
</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
manual slice
manualSlice(array, 1);
pending…
generated slice
slice(array, 1)
pending…
manual toString
manualToString(array);
pending…
generated toString
toString(array)
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