{"action":"created","issue":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/641","labels_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/641/labels{/name}","comments_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/641/comments","events_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/641/events","html_url":"https://github.com/petkaantonov/bluebird/issues/641","id":84283139,"number":641,"title":"Terrible performance with large sequential requests","user":{"login":"vitaly-t","id":5108906,"avatar_url":"https://avatars.githubusercontent.com/u/5108906?v=3","gravatar_id":"","url":"https://api.github.com/users/vitaly-t","html_url":"https://github.com/vitaly-t","followers_url":"https://api.github.com/users/vitaly-t/followers","following_url":"https://api.github.com/users/vitaly-t/following{/other_user}","gists_url":"https://api.github.com/users/vitaly-t/gists{/gist_id}","starred_url":"https://api.github.com/users/vitaly-t/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vitaly-t/subscriptions","organizations_url":"https://api.github.com/users/vitaly-t/orgs","repos_url":"https://api.github.com/users/vitaly-t/repos","events_url":"https://api.github.com/users/vitaly-t/events{/privacy}","received_events_url":"https://api.github.com/users/vitaly-t/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-06-03T00:41:10Z","updated_at":"2015-06-03T14:33:58Z","closed_at":"2015-06-03T07:19:21Z","body":"I've just tested my database library [pg-promise](https://github.com/vitaly-t/pg-promise) for performance versus all popular promise libraries (all latest versions) in a special, sequential test:\r\n\r\n* promise\r\n* bluebird\r\n* vow\r\n* q\r\n* when\r\n* rsvp\r\n* lie\r\n\r\nAll of them in my test showed the same results, give or take 10%, except Bluebird, which takes the remarkable 1,000% to execute the very same test, that's 10 times worse performance than any other promise library that I tested against.\r\n\r\n#### The Test\r\n\r\n```javascript\r\nvar pgpLib = require(\"pg-promise\");\r\n\r\nvar promise = require('vow');\r\n//var promise = require('promise');\r\n//var promise = require('bluebird');\r\n//var promise = require('q');\r\n//var promise = require('when');\r\n//var promise = require('rsvp');\r\n//var promise = require('lie');\r\n\r\nvar cn = \"postgres://user:password@server:port/database\";\r\n\r\nvar pgp = pgpLib({\r\n    promiseLib: promise // choosing which promise library to use;\r\n});\r\n\r\nvar db = pgp(cn);\r\n\r\nfunction factory(idx) {\r\n    if (idx < 10000) {\r\n        return this.none('insert into test(name) values($1)', 'name-' + idx)\r\n    }\r\n}\r\n\r\ndb.tx(function () {\r\n    return promise.all([\r\n        this.none('drop table if exists test'),\r\n        this.none('create table test(id serial, name text)'),\r\n        this.sequence(factory),\r\n        this.one('select count(*) from test')\r\n    ]);\r\n})\r\n    .then(function (data) {\r\n        console.log(\"COUNT:\", data[3].count);\r\n    }, function (reason) {\r\n        console.log(\"REASON:\", reason);\r\n    });\r\n```\r\n\r\nThey key call here is:\r\n```javascript\r\nthis.sequence(factory)\r\n```\r\nwhich has the following implementation:\r\n```javascript\r\n// Sequentially resolves dynamic promises returned by a factory;\r\nfunction sequence(t, factory) {\r\n\r\n    function loop(idx, result) {\r\n        var obj;\r\n        try {\r\n            obj = factory.call(t, idx, t); // get next promise;\r\n        } catch (e) {\r\n            return $p.reject(e);\r\n        }\r\n        if (!obj) {\r\n            // no more promises left in the sequence;\r\n            return $p.resolve(result);\r\n        }\r\n        if (typeof(obj.then) !== 'function') {\r\n            // the result is not a promise;\r\n            return $p.reject(\"Promise factory returned invalid result for index \" + idx);\r\n        }\r\n        return obj.then(function (data) {\r\n            result.push(data);\r\n            return loop(++idx, result);\r\n        }, function (reason) {\r\n            return $p.reject(reason);\r\n        });\r\n    }\r\n\r\n    return loop(0, []);\r\n}\r\n```\r\n\r\nHow is it possible that this method executes 10 slower with Bluebird than with any other library - I can't understand.\r\n\r\nHopefully somebody else will shed light here. But when it is 1 out of 7 promise libraries tested, something tells me the problem is most likely in Bluebird.\r\n\r\nBy the way, the original purpose of the test wasn't in testing the promise libraries, it was to test the performance when inserting 1,000 records vs 10,000, vs 100,000, vs 1,000,000. The issue with the promise came out of nowhere, unexpectedly.\r\nAnd the result was - it takes about 1 min (+/- 10 seconds) to insert 1,000,000 records this way with any promise library, except Bluebird, which takes 10 minutes.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/comments/108457575","html_url":"https://github.com/petkaantonov/bluebird/issues/641#issuecomment-108457575","issue_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/641","id":108457575,"user":{"login":"vitaly-t","id":5108906,"avatar_url":"https://avatars.githubusercontent.com/u/5108906?v=3","gravatar_id":"","url":"https://api.github.com/users/vitaly-t","html_url":"https://github.com/vitaly-t","followers_url":"https://api.github.com/users/vitaly-t/followers","following_url":"https://api.github.com/users/vitaly-t/following{/other_user}","gists_url":"https://api.github.com/users/vitaly-t/gists{/gist_id}","starred_url":"https://api.github.com/users/vitaly-t/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vitaly-t/subscriptions","organizations_url":"https://api.github.com/users/vitaly-t/orgs","repos_url":"https://api.github.com/users/vitaly-t/repos","events_url":"https://api.github.com/users/vitaly-t/events{/privacy}","received_events_url":"https://api.github.com/users/vitaly-t/received_events","type":"User","site_admin":false},"created_at":"2015-06-03T14:33:58Z","updated_at":"2015-06-03T14:33:58Z","body":"@benjamingr , very funny."}}