Created
April 3, 2010 16:07
-
-
Save DracoBlue/354628 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* NodeJS Test Case for the blog post: | |
* "What's faster: JSON.parse+stringify or extend/mixin?" | |
* http://dracoblue.net/dev/whats-faster-jsonparsestringify-or-extendmixin/166/ | |
*/ | |
extend = process.mixin; | |
extend(true, {}, {}) | |
var sys = require("sys"); | |
var test_object = {}; | |
var benchmark = function(function_name, function_object) { | |
var before_timestamp = new Date().getTime(); | |
function_object(); | |
var after_timestamp = new Date().getTime(); | |
sys.puts(" " + Math.floor((after_timestamp - before_timestamp)) + 'ms ' + function_name); | |
} | |
var a_key = "", b_key = "", c_key = "", d_key = ""; | |
for (var i=0; i < 1; i++) { | |
a_key = "a" + Math.floor(i / 100000); | |
b_key = "b" + Math.floor(i / 10000); | |
c_key = "c" + Math.floor(i / 1000); | |
d_key = "d" + Math.floor(i / 100); | |
test_object[a_key] = test_object[a_key] || {}; | |
test_object[a_key][b_key] = test_object[a_key][b_key] || {}; | |
test_object[a_key][b_key][c_key] = test_object[a_key][b_key][c_key] || {}; | |
test_object[a_key][b_key][c_key][d_key] = test_object[a_key][b_key][c_key][d_key] || {}; | |
test_object[a_key][b_key][c_key][d_key]["e" + i] = i + "v"; | |
} | |
sys.puts("JSON-String: " + JSON.stringify(test_object).length + " characters"); | |
benchmark("3000x JSON.parse(JSON.stringify(object))", function() { | |
for (var k=0; k<3000;k++) { | |
JSON.parse(JSON.stringify(test_object)); | |
} | |
}); | |
benchmark("3000x extend(true, {}, object)", function() { | |
for (var k=0; k<3000;k++) { | |
extend(true, {}, test_object); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment