Printo vs Stringify (v2)

Revision 2 of this benchmark created on


Setup

x = ["Hello World", 12, -32, 3.1415, NaN, null, true, false, [1, 3, 4, 5], new Array(),
    {
      foo: 1,
      bar: 2
    }, {
      a: {
        b: "B",
        c: {
          d: "D",
          e: "E",
          f: "F"
        }
      },
      g: "G",
      h: "H",
      json: {
        "web-app": {
          "servlet": [{
            "servlet-name": "cofaxCDS",
            "servlet-class": "org.cofax.cds.CDSServlet",
            "init-param": {
              "configGlossary:installationAt": "Philadelphia, PA",
              "configGlossary:adminEmail": "ksm@pobox.com",
              "configGlossary:poweredBy": "Cofax",
              "configGlossary:poweredByIcon": "/images/cofax.gif",
              "configGlossary:staticPath": "/content/static",
              "templateProcessorClass": "org.cofax.WysiwygTemplate",
              "templateLoaderClass": "org.cofax.FilesTemplateLoader",
              "templatePath": "templates",
              "templateOverridePath": "",
              "defaultListTemplate": "listTemplate.htm",
              "defaultFileTemplate": "articleTemplate.htm",
              "useJSP": false,
              "jspListTemplate": "listTemplate.jsp",
              "jspFileTemplate": "articleTemplate.jsp",
              "cachePackageTagsTrack": 200,
              "cachePackageTagsStore": 200,
              "cachePackageTagsRefresh": 60,
              "cacheTemplatesTrack": 100,
              "cacheTemplatesStore": 50,
              "cacheTemplatesRefresh": 15,
              "cachePagesTrack": 200,
              "cachePagesStore": 100,
              "cachePagesRefresh": 10,
              "cachePagesDirtyRead": 10,
              "searchEngineListTemplate": "forSearchEnginesList.htm",
              "searchEngineFileTemplate": "forSearchEngines.htm",
              "searchEngineRobotsDb": "WEB-INF/robots.db",
              "useDataStore": true,
              "dataStoreClass": "org.cofax.SqlDataStore",
              "redirectionClass": "org.cofax.SqlRedirection",
              "dataStoreName": "cofax",
              "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
              "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
              "dataStoreUser": "sa",
              "dataStorePassword": "dataStoreTestQuery",
              "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
              "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
              "dataStoreInitConns": 10,
              "dataStoreMaxConns": 100,
              "dataStoreConnUsageLimit": 100,
              "dataStoreLogLevel": "debug",
              "maxUrlLength": 500
            }
          }, {
            "servlet-name": "cofaxEmail",
            "servlet-class": "org.cofax.cds.EmailServlet",
            "init-param": {
              "mailHost": "mail1",
              "mailHostOverride": "mail2"
            }
          }, {
            "servlet-name": "cofaxAdmin",
            "servlet-class": "org.cofax.cds.AdminServlet"
          },
    
          {
            "servlet-name": "fileServlet",
            "servlet-class": "org.cofax.cds.FileServlet"
          }, {
            "servlet-name": "cofaxTools",
            "servlet-class": "org.cofax.cms.CofaxToolsServlet",
            "init-param": {
              "templatePath": "toolstemplates/",
              "log": 1,
              "logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
              "logMaxSize": "",
              "dataLog": 1,
              "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
              "dataLogMaxSize": "",
              "removePageCache": "/content/admin/remove?cache=pages&id=",
              "removeTemplateCache": "/content/admin/remove?cache=templates&id=",
              "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
              "lookInContext": 1,
              "adminGroupID": 4,
              "betaServer": true
            }
          }],
          "servlet-mapping": {
            "cofaxCDS": "/",
            "cofaxEmail": "/cofaxutil/aemail/*",
            "cofaxAdmin": "/admin/*",
            "fileServlet": "/static/*",
            "cofaxTools": "/tools/*"
          },
    
          "taglib": {
            "taglib-uri": "cofax.tld",
            "taglib-location": "/WEB-INF/tlds/cofax.tld"
          }
        }
      }
    }, [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20, "DEEP"]]]]]]]]]]]]]]]]]]]]
    
    ];
    
    function json(obj) {
      return JSON.stringify(obj, null, "\t");
    }
    
    
    
    
    /**
     * Print a Object or Array to a tree
     * @param {Object} obj Object or Array to print
     * @param {string} linebreak (optional)
     * @param {string} tabulation (optional)
     * @return {string}
     */
    
    function printo(obj, linebreak, tabulation) {
    
      var n = (linebreak) ? (linebreak) : ("\n");
      var tabtoken = (tabulation) ? (tabulation) : ("\t");
    
      var text = "";
      var level = 0; //initial tabulation level
      var tipo = kindOf(obj);
    
      if (tipo === "object" || tipo === "array") { //check if 'obj' is a object/array
        if (tipo == 'array') tipo += "[" + obj.length + "]"; //append length to array
        text += tab(level) + tipo + n + tab(level) + "(" + n;
        text = expandNode(obj); //inicia a varredura
      } else { //not object/array just an primitive variable
        text = "variable " + tipo + " => " + obj;
      }
    
      function expandNode(obj) {
    
        level++; //go deeper ---->
        for (var node in obj) {
    
          tipo = kindOf(obj[node]);
    
          if (tipo === "object" || tipo === "array") { //node is an Object (does have children)
            if (tipo == 'array') tipo += "[" + obj[node].length + "]"; //append length to array
            text += tab(level) + "[" + node + "] " + tipo + " => " + n; //print parent
            text += tab(level) + "(" + n; //open a family bracket
            text = expandNode(obj[node]); //print children using recursion
          } else { //obj is a property (don't have children)
            text += tab(level);
            text += "[" + node + "] " + tipo + " => " + obj[node] + n;
          }
    
        } //end of family (all children of family is discovered)
        level--; // go up  <----
        text += tab(level) + ")" + n; //close family bracket
        text += tab(level) + n; //identation
        return (text); //return family
      }
    
      return (text); //return full tree
    
      function tab(n) {
        var str = "";
        while (n > 0) {
          str += tabtoken;
          n--;
        }
        return (str);
      }
    
      function kindOf(variable) {
        var type;
        if (variable === null) {
          type = 'null';
        } else if (typeof variable === 'number') {
          type = 'number';
        } else if (Object.prototype.toString.call(variable) === '[object Array]') {
          type = 'array';
        } else {
          type = typeof variable;
        }
        return (type);
      }
    }
    
    
    
    /**
     * Print a Object or Array to a tree
     * @param {Object} obj Object or Array to print
     * @param {string} linebreak (optional)
     * @param {string} tabulation (optional)
     * @return {string}
     */
    
    function printo2(obj, linebreak, tabulation) {
    
      var n = (linebreak) ? (linebreak) : ("\n");
      var tabtoken = (tabulation) ? (tabulation) : ("\t");
    
      var text = "";
      var level = 0; //initial tabulation level
      var tipo = kindOf(obj);
    
      if (tipo === "object" || tipo === "array") { //check if 'obj' is a object/array
        if (tipo == 'array') tipo += "[" + obj.length + "]"; //append length to array
        text += tab(level) + tipo + n + tab(level) + "(" + n;
        text = expandNode(obj, 0, n, tabtoken); //inicia a varredura
      } else { //not object/array just an primitive variable
        text = "variable " + tipo + " : " + obj;
      }
    
      function expandNode(obj, level, n, tabtoken) {
    
        //level++; //go deeper ---->
        var t = tab(level, tabtoken);
    
        for (var node in obj) {
    
          tipo = kindOf(obj[node]);
    
          if (tipo === "object" || tipo === "array") { //node is an Object (does have children)
            if (tipo == 'array') tipo += "[" + obj[node].length + "]"; //append length to array
            text += t + "[" + node + "] " + tipo + " : " + n; //print parent
            text += t + "(" + n; //open a family bracket
            text = expandNode(obj[node], level + 1, n, tabtoken); //print children using recursion
          } else { //obj is a primitive (don't have children)
            text += t;
            text += "[" + node + "] " + tipo + " : " + obj[node] + n;
          }
    
        } //end of family (all children of family is discovered)
        //level--; // go up  <----
        text += t + ")" + n; //close family bracket
        text += t + n; //identation
        return (text); //return family
      }
    
      return (text); //return full tree
    
      function tab(n, tabtoken) {
        var str = "";
        while (n > 0) {
          str += tabtoken;
          n--;
        }
        return str;
      }
    
      function kindOf(variable) {
        var type;
        if (variable === null) {
          type = 'null';
        } else if (typeof variable === 'number') {
          type = 'number';
        } else if (Object.prototype.toString.call(variable) === '[object Array]') {
          type = 'array';
        } else {
          type = typeof variable;
        }
        return (type);
      }
    }
    
    
    
    
    /**
     * Print a Object or Array to a tree
     * @param {Object} obj Object or Array to print
     * @param {string} linebreak (optional)
     * @param {string} tabulation (optional)
     * @return {string}
     */
    function printo3(obj,linebreak,tabulation) {
        
        var n = (linebreak)?(linebreak):("\n");
        var tabtoken = (tabulation)?(tabulation):("|\t");
        
        var level = 0; //initial tabulation level
        
        return expandNode({'var':obj},level,n,tabtoken);
        
        function expandNode(obj,level,n,tabtoken) {
                
                var t=tab(level,tabtoken);
                var  text="";
                
                for (var node in obj) {
                        var value = obj[node];
                        var tipo = kindOf(obj[node]);
                        
                        if (tipo==="object" || tipo==="array") { //node is an Object (does have children)
                                
                                if(tipo=='array') tipo+="["+obj[node].length+"]"; //append length to array
                                
                                text += t + node + " : " +tipo+ " ( " +n; //print parent
                                text += expandNode(value, level+1,n,tabtoken); //print children using recursion
                                text += t + ")" + n; //close object bracket
                                text += t + n;
                        }
    
                        else { //obj is a primitive (don't have children)
                                if(tipo=='string') value='"'+value+'"';
                                text += t + ""+node+" : "+tipo+" => " + value + n; }
                
                } //end of family (all children of family is discovered)
                
                
                return (text); //return family
        }       
        
        function tab(n,tabtoken){
                var str="";
                while (n-->0) {
                        str += tabtoken;
                }
                return str;
        }
        
        function kindOf(variable){
                var type;
                if (variable===null) { type='null'; }
                else if (typeof variable==='number') { type='number'; }
                else if (Object.prototype.toString.call(variable)==='[object Array]'){ type='array'; } 
                else { type=typeof variable; }
                return(type);
        }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
printo
printo(x)
ready
stringify
json(x)
ready
printo2
printo2(x)
ready
printo3
printo3(x)
ready

Revisions

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