Parsing json from data attrs

JavaScript performance comparison

Test case created by JT

Info

Testing performance of parsing json from data attributes

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="single" data-json='{"a":"a","b":"b","c":"c"}'>
</div>
<div id="double" data-json="{'a':'a','b':'b','c':'c'}">
</div>
<div id="split" data-split="a|b|c|d|e">
</div>
<script>
Benchmark.prototype.setup = function() {
    var $single = $('#single'),
        $double = $('#double'),
        $split  = $('#split')
};
</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
Single data
$single.data('json').c;
pending…
Single attr
$.parseJSON($single.attr('data-json')).c;
pending…
Double attr replace
$.parseJSON($double.attr('data-json').replace(/'/g, '"')).c;
pending…
Split Data
$split.data('split').split('|')[2]
pending…
Split Attr
$split.attr('data-split').split('|')[2]
pending…

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

Compare results of other browsers

0 comments

Add a comment