Hello everyone,
TL;DR: we should implement an individual message passing way to avoid
binding application messages with DOM events. This is good for both
performance and desiging.
Tim and I always wonder how slower/faster to use JavaScript implement event
emitter (compare to the DOM event). This matters because the current way we
use DOM events are not all for their original intentions, like passing and
handling user's behavior, like 'click' or 'touchmove'. We also use DOM
event as a channel to pass messages among components within one app
(window.dispatchEvent/addEventListener). For example, lockscreen's `unlock`
and `lock` events, and window manager's `showwindow` or `hidewindow`. These
events are unrrelevant to DOM elements, and the window can be totally
replaced by a 'purer' version of event emitter.
So we choose the EventEmitter.js as a pure JS emitter (why invent our
wheels while there're so many on the market?), and wrote a simple test for
it on jsPerf:
http://jsperf.com/eventemitter-js-vs-dom-events/5
*Caveats*: I wrote this in a hurry, so if this test is not fair, please
patch it or correct me.
This test would create 100 callbacks on both EventEmitter and window, and
the thing it completes is to get the current timestamp and calculate the
interval. I think it performance a basic manipulation that 1. declar local
variable 2. use some API 3. do some calc. Even though I didn't let write
the result out.
Result: DOM version is slower than EventEmitter version significantly. You
can run the demo to get yourown data on your local console. My data shows
Ops/sec is closed to:
EE: 17,100
DOM: 4,300
On Firefox Nightly 29. Google Chrome given similar results.
In conclusion, I think we need a individual way to pass messages. Even this
is hard to be true to have a native version, like what Node.js' module can
use, in our JS engine or browser APIs (discussions, proposal, quarrel,
fighting, discussion, proposal...), we can still do this firstly in Gaia.
And even if my benchmark isn't fair enough and DOM events are actually
faster than JS EE, we may still need to do this. Because mixing application
messages with DOM events are actually binding two unrrelevant notificaitons
together, and it's cause ambiguous design.
(Tim mentioned some features should be done in such EE, like follow the
current `preventDefault` way we used to cancel or supress some events. I
think these are issues need to be solved, but not obstacles that can't be
conquered).
--
<
http://about.me/snowmantw>Greg Weng
http://about.me/snowmantw
*Understand y f = f [ y f ] ; lose last remaining non-major friend*
* -- Anonymous*