Thursday, April 07, 2011

JSON parsing speed in various Node.JS versions

We use Node.JS for a very high capacity service at the Rubicon Project. It often drives or handles in excess of 10B HTTP requests per day sending or receiving JSON data.
Out of curiosity I ran some tests on JSON parsing speed in different versions of Node.JS

node.js code:

var sys = require('sys');
var data = "{ \"item_uuid\": \"8ec56438-d3cf-442a-bbf7-7f076f229f35\", \"return_code\": 0, \"data\": [ { \"valid\": true, \"votes\": 2345, \"date\":\"Thu, 07 Apr 2011 15:17:17 EDT\", \"headline\": \"Senate Majority Leader Harry Reid indicates there likely will be a government shutdown on Friday. Lawmakers have been unable to agree on a new federal budget\", \"source\": \"Yahoo News\", \"published\":{\"hour\":\"19\",\"timezone\":\"UTC\",\"second\":\"17\",\"month\":\"4\",\"minute\":\"17\",\"utime\":\"1302203837\",\"day\":\"7\",\"day_of_week\":\"4\",\"year\":\"2011\"} } ] }";

try {
for(var i = 0; i < 1000000; i++)
var tmp = JSON.parse(data);
} catch(e) { sys.puts("ERROR: on parsing JSON with v8 parser"); }

var tmp = JSON.parse(data);
sys.puts("\n DONE \n");

Essentially this re-parses the same example JSON (I created a fake RSS like JSON pacakge) 1M times.

Here are the results:
  • Node 0.1.3x: real 0m30.050s
  • Node 0.2.6: real 0m30.050s
  • Node 0.3.8: real 0m9.915s
  • Node 0.4.5: real 0m9.999s

For reference I ran the same test against a very fast tokening parser in C called jsmn, and a C++ one called vjson.
  • jsmn: real 0m2.276s
  • vjson: real 0m7.465s Note that vjson is a destructive parser, and I had to fix that first.

Interestingly the JSON parser in node 0.4.5 and prior versions appears to be written in pure Javascript. See the file: node-v0.4.5/deps/v8/src/json.js

It's unclear if the speed improvements are a result of improvements to the parser implementation or in some efficiency/speed leap in versions of V8 included in Node versions.
  • Node 0.1.33: v8: 2010-03-17: Version 2.1.5
  • Node 0.2.6: v8: 2010-08-16: Version 2.3.8
  • Node 0.3.8: v8: 2011-02-02: Version 3.1.1
  • Node 0.4.5: v8: 2011-03-02: Version 3.1.8

Posted via email from aicoder - nealrichter's blog