Created
January 21, 2015 13:05
-
-
Save bitinn/afcd4c6a56fcb10eb21c to your computer and use it in GitHub Desktop.
promise-vs-thunk-in-fast-loop
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
var co = require('co'); | |
//var Promise = require('bluebird'); | |
//var Promise = require('promise'); | |
var count = 0; | |
co(function* () { | |
while (true) { | |
count++; | |
try { | |
yield getData(); | |
//yield getDataThunk(); | |
} catch (err) { | |
console.error(err); | |
} | |
yield sleep(10); | |
if (global.gc) { | |
global.gc(); | |
} | |
if (count % 100 === 0) { | |
console.log('run %d, %smb', count, process.memoryUsage().rss / 1024 / 1024); | |
} | |
if (count > 1000) { | |
return; | |
} | |
} | |
}).catch(function (err) { | |
throw err; | |
}); | |
function sleep(ms) { | |
return function (callback) { | |
setTimeout(callback, ms); | |
}; | |
} | |
function getData() { | |
return new Promise(function(resolve, reject) { | |
resolve(new Array(10000).join('-')); | |
}); | |
} | |
function getDataThunk() { | |
return function (callback) { | |
callback(null, new Array(10000).join('-')); | |
}; | |
} |
// thunk, with gc
df-air:test df$ node --harmony --expose-gc test.js
run 100, 16.9921875mb
run 200, 17.28515625mb
run 300, 17.32421875mb
run 400, 17.42578125mb
run 500, 17.42578125mb
run 600, 17.4296875mb
run 700, 17.4296875mb
run 800, 17.4296875mb
run 900, 17.4296875mb
run 1000, 17.4296875mb
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// then/promise, with gc