JSON speed

JavaScript performance comparison

Revision 4 of this test case created by Test-1

Preparation code

<span id="uniqueId" data-json='{"first":"abc","second":"def","thrid":"ghi"}'>Yo, here I am world!</span>
<script>
Benchmark.prototype.setup = function() {
        function parseJsonX(json) {
                var parsedObj = {};
                json = json.replace(/"|{|}/g,'').split(',');
                for(var l=json.length, i=0;i<l;i++) {
                        var keyValue = json[i].split(':');
                        parsedObj[keyValue[0]] = keyValue[1];
                }
                return parsedObj;
        };
   
        function parseJsonNative(json) {
                    return JSON.parse(json);
        };
        function parseJsonEval(json) {
                    return eval('(' + json + ')');
        };
   
      function parseJsonRegexp(o){
        var f1,f2;
        var store = {};
        if(f1 = o.match(/([^\{\}\,\:\"]+)/g)){
          for(var i=0;i<f1.length;i+=2){
   
            store[ f1[i] ] = f1[i+1];
          }
        }
        return store;
      };
   
      function parseJsonFunction(o){
        return (new Function("return " + o))();
      }
};
</script>

Preparation code output

Yo, here I am world!

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
X-browser parsing
var spanEl = document.getElementById('uniqueId');
var obj = parseJsonX(spanEl.getAttribute('data-json'));
pending…
JSON parsing
var spanEl = document.getElementById('uniqueId');
var obj = parseJsonNative(spanEl.getAttribute('data-json'));
pending…
JSON eval
var spanEl = document.getElementById('uniqueId');
var obj = parseJsonEval(spanEl.getAttribute('data-json'));
pending…
JSON Regexp
var spanEl = document.getElementById('uniqueId');
var obj = parseJsonRegexp(spanEl.getAttribute('data-json'));
pending…
JSON new Function
var spanEl = document.getElementById('uniqueId');
var obj = parseJsonFunction(spanEl.getAttribute('data-json'));
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