Associative array stringify

Benchmark created on


Description

Test of custom associative array stringify routine versus regexp removal of double quotes after JSON.stringify usage

Setup

settings = {
                  selector: 'outerLayout',
                  west: {
                    size: .50,
                    west: {
                      size: 600,
                      html: 'content1',
                      split2V: {
                        1: {
                          size: 400
                        },
                        2: {
                          size: 400
                        }
                      }
                    },
                    east: {
                      size: 800
                    },
                    children: {
                      name: 'middleLayout',
                      center__children: {
                        name: 'innerLayout',
                        id: 'greatStuff',
                        east__size: .33,
                        west__size: .33,
                        autoResize: true,
                        stateManagement__autoLoad: false
                      }
                    }
                  },
                  east: {
                    split2V: {
                      nw: {
                        size: .33,
                        split2H: {
                          size: 300
                        }
                      },
                      ne: {
                        size: 200
                      }
                    },
                    initClosed: true
                  },
                  stateManagement: {
                    enabled: true
                  },
                  tabs: {
                    tab1: {
                      size: 200
                    }
                  },
                  north: {
                    size: .50,
                    west: {
                      size: 600,
                      html: 'content1',
                      split2V: {
                        1: {
                          size: 400
                        },
                        2: {
                          size: 400
                        }
                      }
                    },
                    east: {
                      size: 800
                    },
                    south: {
                      name: 'middleLayout',
                      center__children: {
                        name: 'innerLayout',
                        id: 'greatStuff',
                        east__size: .33,
                        west__size: .33,
                        autoResize: true,
                        stateManagement__autoLoad: false
                      }
                    }
                  },
                  sdlksda: {
                    split2V: {
                      nw: {
                        size: .33,
                        split2H: {
                          size: 300
                        }
                      },
                      ne: {
                        size: 200
                      }
                    },
                    initClosed: true
                  },
                  awdd: {
                    enabled: true
                  },
                  adaawd: {
                    awdwd: {
                      size: 200
                    }
                  },
                  adad: {
                    p1: {
                      size: 300,
                      name: "glkg"
                    },
                    p2: {
                      size: 300,
                      name: "glkg"
                    }
                  },
                  awdad: {
                    size: 200
                  }
                }
    
    
                function customLoop() {
                  function isObject(val) {
                    return val === Object(val);
                  }
    
                  function isEmpty(val) {
                    if (val === undefined || val === null) {
                      return true;
                    }
    
                    if (Array.isArray(val) || typeof val === 'string') {
                      return val.length === 0;
                    }
    
                    for (var key in val) {
                      if (Object.prototype.hasOwnProperty.call(val, key)) {
                        return false;
                      }
                    }
    
                    return true;
                  }
    
                  function stringifyObject(val, opts, pad) {
                    var cache = [];
    
                    return (function stringify(val, opts, pad) {
                      var objKeys;
                      opts = opts || {};
                      opts.indent = opts.indent || '\t';
                      pad = pad || '';
    
                      if (typeof val === 'number' ||
                        typeof val === 'boolean' ||
                        val === null ||
                        val === undefined) {
                        return val;
                      }
    
                      if (Array.isArray(val)) {
                        if (isEmpty(val)) {
                          return '[]';
                        }
    
                        return '[\n' + val.map(function(el, i) {
                          var eol = val.length - 1 === i ? '\n' : ',\n';
                          return pad + opts.indent + stringify(el, opts, pad + opts.indent) + eol;
                        }).join('') + pad + ']';
                      }
    
                      if (isObject(val)) {
                        if (cache.indexOf(val) !== -1) {
                          return null;
                        }
    
                        if (isEmpty(val)) {
                          return '{}';
                        }
    
                        cache.push(val);
    
                        objKeys = Object.keys(val);
    
                        return '{\n' + objKeys.map(function(el, i) {
                          var eol = objKeys.length - 1 === i ? '\n' : ',\n';
                          // quote key if the first character is not `a-z` or
                          // the rest contains something other than `a-z0-9_`
                          // TODO: Find out why this don't work: `/^[^a-z_\$]|\W+/ig`
                          var key = /^[^a-z_]|\W+/ig.test(el) && el[0] !== '$' ? stringify(el, opts) : el;
                          return pad + opts.indent + key + ': ' + stringify(val[el], opts, pad + opts.indent) + eol;
                        }).join('') + pad + '}';
                      }
    
                      if (opts.singleQuotes === false) {
                        return '"' + val.replace(/"/g, '\\\"') + '"';
                      } else {
                        return "'" + val.replace(/'/g, "\\\'") + "'";
                      }
                    })(val, opts, pad);
                  }
                  var stringify = stringifyObject(settings, {
                    indent: '',
                    singleQuotes: false
                  });
                  return stringify;
                }
    
                function regExp() {
                  json = JSON.stringify(settings);
                  jsonb = json.replace(/(["])(\w+)(["]:)/g, '$2:');
                  return jsonb;
                }

Test runner

Ready to run.

Testing in
TestOps/sec
Custom assoc. array stringify method
customLoop(settings)
ready
JSON.stringify method
regExp(settings)
ready

Revisions

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