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
Author
// then/promise, with gc
df-air:test df$ node --harmony --expose-gc test.js
run 100, 17.0078125mb
run 200, 17.35546875mb
run 300, 17.51171875mb
run 400, 17.6015625mb
run 500, 17.609375mb
run 600, 17.5625mb
run 700, 17.5703125mb
run 800, 17.5625mb
run 900, 17.5703125mb
run 1000, 17.65234375mb
Author
// 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
// bluebird promise, with gc