Skip to content

Instantly share code, notes, and snippets.

@robertklep
Created May 23, 2013 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertklep/5634851 to your computer and use it in GitHub Desktop.
Save robertklep/5634851 to your computer and use it in GitHub Desktop.
function sync() {
var myData = '';
var DateObj = new Date();
for (var i=0; i<50000; i++) {
myData += "'" + DateObj.toString() +"', ";
}
return myData;
}
function async(DateObj, myData, i, cb) {
if (i === 50000)
return cb(null, myData);
myData += "'" + DateObj.toString() +"', ";
setImmediate(function() {
async(DateObj, myData, i + 1, cb);
});
}
console.time('sync');
var syncresult = sync();
console.timeEnd('sync');
console.time('async');
async(new Date(), '', 0, function(err, asyncresult) {
console.timeEnd('async');
console.log('Same result for both?', asyncresult === syncresult);
});
@amolmk
Copy link

amolmk commented May 23, 2013

setImmediate(function() {
^
ReferenceError: setImmediate is not defined
at async (D:\New folder\app.js:14:1)
at Object. (D:\New folder\app.js:24:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment