eval test

JavaScript performance comparison

Test case created by JP Grace

Info

I created this page to test the performance variation of a certain function with and without eval.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var x = '0', y = '1';
    evalTest = function (x, y) {
        'use strict';
   
        // Match any character except: digits (0-9), dash (-), period (.), or backslash (/) and replace those characters with empty string.
        x = x.replace(/[^\d\-\.\/]/g, '');
        y = y.replace(/[^\d\-\.\/]/g, '');
   
        // Handle simple fractions
        if (x.indexOf('/') >= 0) {
                x = eval(x);
        }
        if (y.indexOf('/') >= 0) {
                y = eval(y);
        }
   
        return x - y;
    };
    notEvalTest = function (x, y) {
                'use strict';
   
        // Define vars
        var a = [], b = [];
   
        // Match any character except: digits (0-9), dash (-), period (.), or backslash (/) and replace those characters with empty string.
        x = x.replace(/[^\d\-\.\/]/g, '');
        y = y.replace(/[^\d\-\.\/]/g, '');
   
        // Handle simple fractions
        if (x.indexOf('/') >= 0) {
                a = x.split("/");
                x = parseInt(a[0], 10) / parseInt(a[1], 10);
        }
        if (y.indexOf('/') >= 0) {
                b = y.split("/");
                y = parseInt(b[0], 10) / parseInt(b[1], 10);
        }
   
        return x - y;
    };
};
</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
evalTest(x, y);
pending…
NOT eval
notEvalTest(x, y);
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