Skip to content

Instantly share code, notes, and snippets.

@j2labs
Created June 23, 2011 16:34
Show Gist options
  • Select an option

  • Save j2labs/1042932 to your computer and use it in GitHub Desktop.

Select an option

Save j2labs/1042932 to your computer and use it in GitHub Desktop.
JSON speed test in Javascript / Node.js (not sure who to attribute for this speed)
/*
* A typical run looks like this:
*
* Dumping:
* JSON: 2.017
* Loading:
* JSON: 1.93
* eval(): 25.387
*/
/*
* Prepare data structures
*/
var data_as_array = {
foo: "bar",
food: "barf",
good: "bars",
dood: "where your car?",
"wheres your car": "dude"
};
var data_as_json = JSON.stringify(data_as_array);
/*
* Time dumping to string
*/
console.log("Dumping:");
var start = new Date();
for(i=0; i<1000000; i++) {
JSON.stringify(data_as_array);
}
var fin = new Date();
var run_time = (fin - start) / 1000; /* in seconds */
console.log(" JSON: " + run_time);
/*
* Time loading into native
*/
console.log("Loading:");
var start = new Date();
for(i=0; i<1000000; i++) {
JSON.parse(data_as_json);
}
var fin = new Date();
var run_time = (fin - start) / 1000; /* in seconds */
console.log(" JSON: " + run_time);
var start = new Date();
var data_for_eval = '(' + data_as_json + ')';
for(i=0; i<1000000; i++) {
eval(data_for_eval);
}
var fin = new Date();
var run_time = (fin - start) / 1000; /* in seconds */
console.log(" eval(): " + run_time);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment