Click here to Skip to main content
15,883,812 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 356.9K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
// var BSON = require('../../lib/mongodb').BSONNative.BSON,
//   ObjectID = require('../../lib/mongodb').BSONNative.ObjectID,
//   Code = require('../../lib/mongodb').BSONNative.Code,
//   Long = require('../../lib/mongodb').BSONNative.Long,
//   Binary = require('../../lib/mongodb').BSONNative.Binary,
//   debug = require('util').debug,
//   inspect = require('util').inspect,
// 
//   Long = require('../../lib/mongodb').Long,
//   ObjectID = require('../../lib/mongodb').ObjectID,
//   Binary = require('../../lib/mongodb').Binary,
//   Code = require('../../lib/mongodb').Code,  
//   DBRef = require('../../lib/mongodb').DBRef,  
//   Symbol = require('../../lib/mongodb').Symbol,  
//   Double = require('../../lib/mongodb').Double,  
//   MaxKey = require('../../lib/mongodb').MaxKey,  
//   MinKey = require('../../lib/mongodb').MinKey,  
//   Timestamp = require('../../lib/mongodb').Timestamp;
  

// var BSON = require('../../lib/mongodb').BSONPure.BSON,
//   ObjectID = require('../../lib/mongodb').BSONPure.ObjectID,
//   Code = require('../../lib/mongodb').BSONPure.Code,
//   Long = require('../../lib/mongodb').BSONPure.Long,
//   Binary = require('../../lib/mongodb').BSONPure.Binary;

var BSON = require('../lib/bson').BSONNative.BSON,
	Long = require('../lib/bson').Long,
	ObjectID = require('../lib/bson').ObjectID,
	Binary = require('../lib/bson').Binary,
	Code = require('../lib/bson').Code,  
	DBRef = require('../lib/bson').DBRef,  
	Symbol = require('../lib/bson').Symbol,  
	Double = require('../lib/bson').Double,  
	MaxKey = require('../lib/bson').MaxKey,  
	MinKey = require('../lib/bson').MinKey,  
	Timestamp = require('../lib/bson').Timestamp;

  // console.dir(require('../lib/bson'))

var COUNT = 1000;
var COUNT = 100;

var object = {
  string: "Strings are great",
  decimal: 3.14159265,
  bool: true,
  integer: 5,
  date: new Date(),
  double: new Double(1.4),
  id: new ObjectID(),
  min: new MinKey(),
  max: new MaxKey(),
  symbol: new Symbol('hello'),
  long: Long.fromNumber(100),
  bin: new Binary(new Buffer(100)),
  
  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",
  code: new Code("function() {}", {i:1})
}

// Number of objects
var numberOfObjects = 10000;
var bson = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]);
console.log("---------------------- 1")
var s = new Date()
// Object serialized
for(var i = 0; i < numberOfObjects; i++) {
  objectBSON = bson.serialize(object, null, true)  
}
console.log("====================== " + (new Date().getTime() -  s.getTime()) + " :: " + ((new Date().getTime() -  s.getTime()))/numberOfObjects)

console.log("---------------------- 2")
var s = new Date()
// Object serialized
for(var i = 0; i < numberOfObjects; i++) {
  bson.deserialize(objectBSON);  
}
console.log("====================== " + (new Date().getTime() -  s.getTime()) + " :: " + ((new Date().getTime() -  s.getTime()))/numberOfObjects)

// // Buffer With copies of the objectBSON
// var data = new Buffer(objectBSON.length * numberOfObjects);
// var index = 0;
// 
// // Copy the buffer 1000 times to create a strea m of objects
// for(var i = 0; i < numberOfObjects; i++) {
//   // Copy data
//   objectBSON.copy(data, index);
//   // Adjust index
//   index = index + objectBSON.length;
// }
// 
// // console.log("-----------------------------------------------------------------------------------")
// // console.dir(objectBSON)
// 
// var x, start, end, j
// var objectBSON, objectJSON
// 
// // Allocate the return array (avoid concatinating everything)
// var results = new Array(numberOfObjects);
// 
// console.log(COUNT + "x (objectBSON = BSON.serialize(object))")
// start = new Date
// 
// // var objects = BSON.deserializeStream(data, 0, numberOfObjects);
// // console.log("----------------------------------------------------------------------------------- 0")
// // var objects = BSON.deserialize(data);
// // console.log("----------------------------------------------------------------------------------- 1")
// // console.dir(objects)
// 
// for (j=COUNT; --j>=0; ) {  
//   var nextIndex = BSON.deserializeStream(data, 0, numberOfObjects, results, 0);
// }
// 
// end = new Date
// var opsprsecond = COUNT / ((end - start)/1000);
// console.log("bson size (bytes): ", objectBSON.length);
// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec");
// console.log("MB/s = " + ((opsprsecond*objectBSON.length)/1024));
// 
// // console.dir(nextIndex)
// // console.dir(results)


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions