eval-methods

JavaScript performance comparison

Revision 11 of this test case created by rvmn

Info

edited to have a check that it is actually doing what it is supposed to: see the console to view and check the output for each + see the console.time and console.profile tests (all put in benchmark setup, so doesnt influence tests) Be advised though: all except the normal eval AND the last global eval run inside a seperate scope, so you cannot reach any variables or functions from the page(global scope) or !!!

http://moduscreate.com/javascript-performance-tips-tricks/

Preparation code

<script>
var strCodeTest = 'var str="",i=0,j=10;while(i<j){str+="a";i++};console.log(str,';
var strCode = 'var str="",i=0,j=10;while(i<j){str+="a";i++};';
var a = new Blob([strCodeTest+'"5:Blob")']);
var urla = URL.createObjectURL(a);

function loadBloba(){
    var script = document.createElement("script")
    script.type = "text/javascript";
    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
               
            }
        };
    } else {  //Others
        script.onload = function(){
           
        };
    }
    script.src = urla;
    document.getElementsByTagName("head")[0].appendChild(script);
}
console.profile();
console.time('1:eval (not wrapped)');
(function eval_not_wrapped() {
  eval(strCodeTest+'"1:eval (not wrapped)")');
})();
console.timeEnd('1:eval (not wrapped)');
console.time('2:new Function');
(function newFunction() {
  (new Function(strCodeTest+'"2:new Function")'))();
})();
console.timeEnd('2:new Function');
console.time('3:global eval');
(function global_eval() {
  (0,eval)(strCodeTest+'"3:global eval")');
})();
console.timeEnd('3:global eval');
console.time('4:eval (wrapped)');
(function eval_wrapped() {
  eval('(function(){'+strCodeTest+'"4:eval (wrapped)")'+'})()');
})();
console.timeEnd('4:eval (wrapped)');
console.time('5:Blob');
loadBloba();
console.timeEnd('5:Blob');
console.time('6:global non-anon');
(0,eval)(strCodeTest+'"6:global non-anon")');
console.timeEnd('6:global non-anon');
console.profileEnd();
</script>
<script>
Benchmark.prototype.setup = function() {
    var b = new Blob([strCode]);
    var url = URL.createObjectURL(b);
    function loadBlob(){
        var script = document.createElement("script")
        script.type = "text/javascript";
        if (script.readyState){  //IE
            script.onreadystatechange = function(){
                if (script.readyState == "loaded" ||
                        script.readyState == "complete"){
                    script.onreadystatechange = null;
                    deferred.resolve()
                }
            };
        } else {  //Others
            script.onload = function(){
                deferred.resolve()
            };
        }
        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);
    }
};
</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
eval (not wrapped)
eval(strCode);
pending…
new Function
(function() {
  (new Function(strCode))();
})();
pending…
global eval
(function() {
  (0,eval)(strCode);
})();
pending…
eval (wrapped)
(function() {
  eval('(function(){'+strCode+'})()');
})();
pending…
Blob
// async test
loadBlob();
pending…
global scope non-anonymized
(0,eval)(strCode);
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