encodejs

Benchmark created by Enzo on


Setup

encode = function(str) {
    
    
        try {
    
                str = str.replace(/'/g, '"');
                str = str.replace(/\[\s*"([^"]+)"\s*\]/g, '.$1'); // object['property']   ->    object.property
    
                var list = [], match, merge=false, totalchars, i, t, elementproperty, 
    
                prepare_array = function (){
    
                        totalchars += arguments[0].length;
    
                        if (arguments[4] === 0 && (typeof arguments[1] === 'undefined' || arguments[1] === ''))
                                return 0;
    
                        if (typeof list[i] === 'string')
                                list[i] = [];
    
                        if (typeof arguments[2] === 'string' && arguments[2] !== '')
                                list[i].push(Number(arguments[2]));
    
                        else if (typeof arguments[3] === 'string' && arguments[3] !== '')
                                list[i].push( eval('['+arguments[3]+']') );
    
                        else
                                list[i].push( arguments[1] );
    
                        return 1;
    
                };
    
    
    
                // Spliting dots     
                // "From.this.example"   ->   ["From", "this". "example"]
                str.replace(/([^.]+)/g, function(){
    
                        match = arguments[1];
                        if (merge) {
                                merge = false;
                                list[list.length-1] += '.' + match;
                                match = list[list.length-1];
                        }
                        else
                                list.push(match);
    
    
                        if ((match.match(/\(/g) || []).length !== (match.match(/\)/g) || []).length)
                                merge = true;
    
                });
    
    
                for (i=0, t=list.length; i<t; ++i) {
    
                        totalchars = 0;
    
                        elementproperty = list[i];
    
                        if (list[i].replace(/(^[A-Za-z_$][\w_$]*)|(?:\[\s*(\d+)\s*\])|(?:\((.*)\))/g, prepare_array).indexOf('0') > -1 ||
                                elementproperty.length !== totalchars)
                                return false;
                }
    
    
                return list;
    
        }
    
        catch(e) {
    
                return false;
    
        }
    
    
    };

Test runner

Ready to run.

Testing in
TestOps/sec
simple
encode('hola')
ready
complex
encode('User.filter[25]({ idgroup: 95, active: true, test: 25, try: 123 },123, 123,123, 123, 123)[2][0].order_by[1][2][3]("name", "created")[4][5].order[ 0][1][  "lol" ].order["tes.t"].order("t.est").onemore(function(){return 1}).hgh')
 
ready

Revisions

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