Skip to content

Instantly share code, notes, and snippets.

@marcello3d
Created November 14, 2011 15:22
Show Gist options
  • Save marcello3d/1364159 to your computer and use it in GitHub Desktop.
Save marcello3d/1364159 to your computer and use it in GitHub Desktop.
mongodb-native vs node-buffalo benchmark
var BSON = require('../buffalo'),
BSONPure = require('mongodb').BSONPure.BSON//,
// BSONNative = require('mongodb').BSONNative.BSON
var COUNT = 100000
var object = {
string: "Strings are great",
decimal: 3.14159265,
bool: true,
integer: 5,
subObject: {
moreText: "Bacon ipsum dolor sit amet cow pork belly rump ribeye pastrami andouille. Tail hamburger pork belly, drumstick flank salami t-bone sirloin pork chop ribeye ham chuck pork loin shankle. Ham fatback pork swine, sirloin shankle short loin andouille shank sausage meatloaf drumstick. Pig chicken cow bresaola, pork loin jerky meatball tenderloin brisket strip steak jowl spare ribs. Biltong sirloin pork belly boudin, bacon pastrami rump chicken. Jowl rump fatback, biltong bacon t-bone turkey. Turkey pork loin boudin, tenderloin jerky beef ribs pastrami spare ribs biltong pork chop beef.",
longKeylongKeylongKeylongKeylongKeylongKey: "Pork belly boudin shoulder ribeye pork chop brisket biltong short ribs. Salami beef pork belly, t-bone sirloin meatloaf tail jowl spare ribs. Sirloin biltong bresaola cow turkey. Biltong fatback meatball, bresaola tail shankle turkey pancetta ham ribeye flank bacon jerky pork chop. Boudin sirloin shoulder, salami swine flank jerky t-bone pork chop pork beef tongue. Bresaola ribeye jerky andouille. Ribeye ground round sausage biltong beef ribs chuck, shank hamburger chicken short ribs spare ribs tenderloin meatloaf pork loin."
},
subArray: [1,2,3,4,5,6,7,8,9,10],
anotherString: "another string"
}
var x, start, end, i
var serializedBSON, serializedBSONPure, serializedBSONNative, serializedJSON
var deserializedBSON, deserializedBSONPure, deserializedBSONNative, deserializedJSON
start = new Date
for (i=COUNT; --i>=0; ) {
serializedBSON = BSON.serialize(object)
}
end = new Date
console.log(COUNT + "x (serializedBSON = BSON.serialize(object)) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
start = new Date
for (i=COUNT; --i>=0; ) {
serializedBSONPure = BSONPure.serialize(object, null, true)
}
end = new Date
console.log(COUNT + "x (serializedBSONPure = BSONPure.serialize(object)) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
//start = new Date
//for (i=COUNT; --i>=0; ) {
// serializedBSONNative = BSONNative.serialize(object, null, true)
//}
//end = new Date
//console.log(COUNT + "x (serializedBSONNative = BSONNative.serialize(object)) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
start = new Date
for (i=COUNT; --i>=0; ) {
serializedJSON = JSON.stringify(object)
}
end = new Date
console.log(COUNT + "x (serializedJSON = JSON.stringify(object)) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
console.log("BSON size (bytes):", serializedBSON.length)
console.log("serializedBSON == serializedBSONPure ? "+compare(serializedBSON,serializedBSONPure))
//console.log("serializedBSON == serializedBSONNative ? "+compare(serializedBSON,serializedBSONNative))
//console.log("serializedBSONPure == serializedBSONNative ? "+compare(serializedBSONNative,serializedBSONPure))
start = new Date
for (i=COUNT; --i>=0; ) {
deserializedBSON = BSON.parse(serializedBSON)
}
end = new Date
console.log(COUNT + "x BSON.parse(serializedBSON) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
start = new Date
for (i=COUNT; --i>=0; ) {
deserializedBSONPure = BSONPure.deserialize(serializedBSONPure)
}
end = new Date
console.log(COUNT + "x BSONPure.deserialize(serializedBSONPure) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
//
//start = new Date
//for (i=COUNT; --i>=0; ) {
// deserializedBSONNative = BSONNative.deserialize(serializedBSONNative)
//}
//end = new Date
//console.log(COUNT + "x BSONNative.deserialize(serializedBSONNative) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
start = new Date
for (i=COUNT; --i>=0; ) {
deserializedJSON = JSON.parse(serializedJSON)
}
end = new Date
console.log(COUNT + "x JSON.parse(serializedJSON) time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec")
function compare(b1, b2) {
try {
require('assert').deepEqual(b1,b2)
return true
} catch (e) {
console.error(e)
return false
}
}
console.log("object == deserializedBSON ? "+compare(object,deserializedBSON))
console.log("object == deserializedBSONPure ? "+compare(object,deserializedBSONPure))
//console.log("object == deserializedBSONNative ? "+compare(object,deserializedBSONNative))
console.log("object == deserializedJSON ? "+compare(object,deserializedJSON))
console.log("deserializedBSON == deserializedBSONPure ? "+compare(deserializedBSON,deserializedBSONPure))
//console.log("deserializedBSON == deserializedBSONNative ? "+compare(deserializedBSON,deserializedBSONNative))
//console.log("deserializedBSONPure == deserializedBSONNative ? "+compare(deserializedBSONNative,deserializedBSONPure))
console.log("deserializedJSON == deserializedBSON ? "+compare(deserializedJSON,deserializedBSON))
console.log("deserializedJSON == deserializedBSONPure ? "+compare(deserializedJSON,deserializedBSONPure))
//console.log("deserializedJSON == deserializedBSONNative ? "+compare(deserializedJSON,deserializedBSONNative))
100000x (serializedBSON = BSON.serialize(object)) time = 5445 ms - 18365.472910927456 ops/sec
100000x (serializedBSONPure = BSONPure.serialize(object)) time = 7153 ms - 13980.14818957081 ops/sec
100000x (serializedJSON = JSON.stringify(object)) time = 643 ms - 155520.99533437015 ops/sec
BSON size (bytes): 1387
serializedBSON == serializedBSONPure ? true
100000x BSON.parse(serializedBSON) time = 2020 ms - 49504.9504950495 ops/sec
100000x BSONPure.deserialize(serializedBSONPure) time = 3033 ms - 32970.65611605671 ops/sec
100000x JSON.parse(serializedJSON) time = 745 ms - 134228.1879194631 ops/sec
object == deserializedBSON ? true
object == deserializedBSONPure ? true
object == deserializedJSON ? true
deserializedBSON == deserializedBSONPure ? true
deserializedJSON == deserializedBSON ? true
deserializedJSON == deserializedBSONPure ? true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment