static
static

Reputation: 8396

What is faster event-based or deferred-based handling?

What is faster:

Event.once('some_event', function() { // wait until an event fired from somewhere
  defer.resolve('resolved')
})
...
// somewhere else
Event.emit('some_event')

or smth.like:

defer = doSmthDeferred(); // to get the deferred resolved/rejected from somewhere else
...
// somewhere else
doSmthDeferred = function() {
  defer
  if ...
    defer.resolve();
  else
    defer.reject();
}

I.e.: what is better - event processing or handling with nested promises/deferreds? Are promises/deferreds based on event processing?

Upvotes: 0

Views: 204

Answers (1)

ForbesLindesay
ForbesLindesay

Reputation: 10712

This is pretty much a red herring. Speed differences between events and promise resolution are almost always going to be tiny.

Events are designed for things which happen lots of times. e.g. Requests arriving on a webserver or users clicking a button. They get messy when you use them for asynchronous operations as they don't support composition properly, I.e. It's difficult to wait for two events to both have happened Promises are designed for asynchronous operations (like requesting a web page from a server). They can only be resolved once so are not suitable for event like things. If you have multiple asynchronous operations happening in parallel or series, promises make it really easy to compose them. If you're looking for a good promise library I'd suggest you check out Q

Upvotes: 1

Related Questions