eval vs. <script> (v5)

Revision 5 of this benchmark created by Michael Ficarra on


Setup

var JS = "function fibonacci(n) { \
        if (n < 2) { \
            return 1; \
        } else { \
            return fibonacci(n-2) + fibonacci(n-1); \
        } \
    }";
    
    function fibonacci(n) {
        if (n < 2) {
            return 1;
        } else {
            return fibonacci(n-2) + fibonacci(n-1);
        }
    }
    
    eval(JS.replace(/fibonacci/g, "fibonacciEval"));
    
    (0,eval)(JS.replace(/fibonacci/g, "fibonacciIndirectEval"));
    
    var fibonacciFn1 = Function('n','\
        if (n < 2) {\
            return 1;\
        } else {\
            var fibonacciFn1 = arguments.callee;\
            return fibonacciFn1(n-2) + fibonacciFn1(n-1);\
        }\
    ');
    
    var fibonacciFn2 = Function(JS.replace(/fibonacci/g, "fibonacciFn2") + 'return fibonacciFn2')();
    
    var head = document.getElementsByTagName('head')[0];
    var el = document.createElement("script");
    el.innerHTML = JS.replace(/fibonacci/g, "fibonacciScript");
    head.appendChild(el);

Teardown


    head.removeChild(el);
  

Test runner

Ready to run.

Testing in
TestOps/sec
eval
fibonacciEval(20);
ready
script
fibonacciScript(20);
ready
Function One
fibonacciFn1(20);
ready
Function Two
fibonacciFn2(20);
ready
Normal
fibonacci(20);
ready
indirect eval
fibonacciIndirectEval(20);
ready

Revisions

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

  • Revision 1: published by Devon Govett on
  • Revision 4: published by John-David Dalton on
  • Revision 5: published by Michael Ficarra on
  • Revision 6: published by John-David Dalton on