parseJSON vs $.map vs eval to convert string to ints (v2)

Revision 2 of this benchmark created by patrick dw on


Preparation HTML

<div id="dynamicTable" columns="26,40,41,21,71,39,23,19">test</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var columns = jQuery('#dynamicTable').attr('columns'),
      attributeIds;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
$.parseJSON
attributeIds = jQuery.parseJSON('[' + columns + ']');
ready
$.map
attributeIds = jQuery.map(columns.split(','), function(val, idx) {
 return parseInt(val, 10)
});
ready
eval
attributeIds = eval('[' + columns + ']');
ready
while loop with + operator
attributeIds = columns.split(',');
var len = attributeIds.length;

while (len--) {
 attributeIds[len] = +attributeIds[len];
}
ready

Revisions

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

  • Revision 2: published by patrick dw on