JSON v. JSONH v. msgpack

JavaScript performance comparison

Revision 15 of this test case created by Vlad Mysla

Preparation code

<script src="//cdn.jsdelivr.net/msgpack/1.05/msgpack.base.js"></script>
<script>
var json = [{
          PartnerID               : 1                                
        , APIKey                  : '4f92cd9422044bd6b68f20994cad8bc8'
        , SubsystemId             : 0  
 
        , HttpVerbTypeID          : 1
        , HttpStatus              : 200
 
        , RequestDatetime         : '2014-05-15 19:41:27.016'
 
        , PageURL                 : 'http://www.justanswer.com/law/8g93i-options-someone-used-sealed-court-documents.html'
        , ReferralURL             : 'http://www.justanswer.com/law/'
 
        , RequestGUID             : 'd3144881-8c96-4bb5-b055-68b560fb9436'
        , OriginatingRequestGUID  : ''
 
        , RouteAction             : 'page/unknown'
        , RouteActionGUID         : '214a4f03-95b7-4895-9140-6eb55741cc24'
 
        , VisitorGUID             : '3e7c3ea7-2a9a-4a51-d760-8f72a8714730'
        , ApiEncodedUserID        : 'd2bc0a1283334374b6da7be4fe31f7ab'
 
        , CookiesEnabled          : true
     }];

  var StaticJSONP = {
      notify: function(uid, dummyData) {
        window.dummyData = json;
      }
    };


    var JsonFormatter = {
        stringify: function (cipherParams) {
            // create json object with ciphertext
            var jsonObj = {
                ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
            };

            // optionally add iv and salt
            if (cipherParams.iv) {
                jsonObj.iv = cipherParams.iv.toString();
            }
            if (cipherParams.salt) {
                jsonObj.s = cipherParams.salt.toString();
            }

            // stringify json object
            return JSON.stringify(jsonObj);
        },

        parse: function (jsonStr) {
            // parse json string
            var jsonObj = JSON.parse(jsonStr);

            // extract ciphertext from json object, and create cipher params object
            var cipherParams = CryptoJS.lib.CipherParams.create({
                ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
            });

            // optionally extract iv and salt
            if (jsonObj.iv) {
                cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
            }
            if (jsonObj.s) {
                cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
            }

            return cipherParams;
        }
    };

  "JSON" in this || document.write('<','script src="http://www.3site.eu/examples/json.hpack/test/json2.js">','<','/script>');
  document.write([
      '<','script src="http://www.3site.eu/examples/json.hpack/test/dummy.js">','<','/script>',
      '<','script src="http://www.3site.eu/examples/json.hpack/test/JSONH.js">','<','/script>','<','script src="http://www.javascriptoo.com/application/html/js/tcorral/jsonc/jsonc.js">','<','/script>',
      '<','script src="http://www.3site.eu/examples/json.hpack/test/JSONHF.js">','<','/script>'

,
      '<','script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js">','<','/script>'
,
      '<','script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-utf16-min.js">','<','/script>'
,
      '<','script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js">','<','/script>'
,
      '<','script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">','<','/script>'
  ].join(""));
</script>
 
<script>
Benchmark.prototype.teardown = function() {
    if( typeof(Base64result)!='undefined' && typeof(done)=='undefined'){
    n="JSONHresult";
    console.log(n,window[n],window[n].length);
    n="JSONresult";
    console.log(n,window[n],window[n].length);
    n="msgpackresult";
    console.log(n,window[n],window[n].length);
    n="JSONCresult";
    console.log(n,window[n],window[n].length);
    n="JSONAESresult";
    console.log(n,window[n],window[n].length);
    n="Base64result";
    console.log(n,window[n],window[n].length);
   
    message = window.JSONCresult;
    words = CryptoJS.enc.Utf16.parse(message);
    window.Base64result=CryptoJS.enc.Base64.stringify(words);console.log('JSONC+Base64 encode',window.Base64result,window.Base64result.length);
   
    message = window.JSONCresult;
    words = CryptoJS.enc.Base64.parse(window.Base64result);
    window.Base64result=CryptoJS.enc.Utf16.stringify(words);console.log('JSONC+Base64 decode',window.Base64result,window.Base64result.length);
   
    done = true;
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
JSON.stringify
window.JSONresult = JSON.stringify(dummyData);
 
pending…
JSONH.stringify
window.JSONHresult = JSONH.stringify(dummyData);
 
pending…
msgpack.pack
window.msgpackresult = msgpack.pack(dummyData);
 
pending…
JSON.parse
window.JSONparsed = JSON.parse(JSONresult);
pending…
JSONH.parse
window.JSONHparsed = JSONH.parse(JSONHresult);
pending…
msgpack.unpack
window.msgpackparsed = msgpack.unpack(msgpackresult);
pending…
JSONC.pack
window.JSONCresult = JSONC.pack( dummyData);
 
pending…
JSONC.unpack
window.JSONCparsed = JSONC.unpack(JSONCresult);
pending…
CryptoJS.AES.encrypt
var message = JSON.stringify(json);
var hash =  CryptoJS.AES.encrypt(message,'pass', { format: JsonFormatter });
window.JSONAESresult=hash.toString();
 
pending…
Base64.stringify
var message = JSON.stringify(json);
var words = CryptoJS.enc.Utf8.parse(message);
window.Base64result=CryptoJS.enc.Base64.stringify(words);
 
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