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 hidden or 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('-')); | |
| }; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// thunk, with gc