eval vs. Function vs. script vs. normal (v4)

Revision 4 of this benchmark created by John-David Dalton on


Preparation HTML

<script>
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'));

var fibonacciFn = Function('n','\
    if (n < 2) {\
        return 1;\
    } else {\
        return fibonacciFn(n-2) + fibonacciFn(n-1);\
    }\
');

(function() {
  var head = document.getElementsByTagName('head')[0];
  var el = document.createElement('script');
  el.innerHTML = JS.replace(/fibonacci/g, 'fibonacciScript');
  head.appendChild(el);
}());
</script>

Setup

var fibonacci = window.fibonacci,
        fibonacciEval = window.fibonacciEval,
        fibonacciFn = window.fibonacciFn,
        fibonacciScript = window.fibonacciScript;

Test runner

Ready to run.

Testing in
TestOps/sec
eval
fibonacciEval(20);
ready
script
fibonacciScript(20);
ready
Function
fibonacciFn(20);
ready
normal
fibonacci(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