eval test

Benchmark created by JP Grace on


Description

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

Setup

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;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
eval
evalTest(x, y);
ready
NOT eval
notEvalTest(x, y);
ready

Revisions

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

  • Revision 1: published by JP Grace on
  • Revision 2: published by Praneet Loke on
  • Revision 3: published by G3z on