1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
var async = require("async");
var modules = {
module1:{
event1: function(){for (var i = 0; i < 100; ++i);},
event2: function(){for (var i = 0; i < 100; ++i);}
},
module2:{
init: function(){for (var i = 0; i < 100; ++i);},
event2: function(){for (var i = 0; i < 100; ++i);}
},
module3:{
init: function(){for (var i = 0; i < 100; ++i);},
event1: function(){for (var i = 0; i < 100; ++i);}
},
module4:{
init: function(){for (var i = 0; i < 100; ++i);},
event1: function(){for (var i = 0; i < 100; ++i);},
event2: function(){for (var i = 0; i < 100; ++i);}
}
};
var EventEmitter = require('events').EventEmitter;
var maxIteration = 1000000;
function Test1()
{
var Test = function(){
};
Test.prototype = new EventEmitter;
var Tester = new Test();
Tester.on('init', function(){
var module;
for (module in modules)
if (typeof(modules[module]['init']) === 'function')
modules[module]['init']();
});
Tester.on('event1', function(){
var module;
for (module in modules)
if (typeof(modules[module]['event1']) === 'function')
modules[module]['event1']();
});
Tester.on('event2', function(){
var module;
for (module in modules)
if (typeof(modules[module]['event2']) === 'function')
modules[module]['event2']();
});
console.time('Test #1');
Tester.emit('init');
for (var i = 0; i < maxIteration; ++i)
{
Tester.emit('event1');
Tester.emit('event2');
}
console.timeEnd('Test #1');
}
function Test2()
{
var Test = function(){
};
Test.prototype = new EventEmitter;
var Tester = new Test();
var callModule = function(mtdName)
{
var module;
for (module in modules)
if (typeof(modules[module][mtdName]) === 'function')
modules[module][mtdName]();
};
Tester.on('init', function(){callModule('init');});
Tester.on('event1', function(){callModule('event1');});
Tester.on('event2', function(){callModule('event2');});
console.time('Test #2');
Tester.emit('init');
for (var i = 0; i < maxIteration; ++i)
{
Tester.emit('event1');
Tester.emit('event2');
}
console.timeEnd('Test #2');
}
function Test3()
{
var Test = function(){
};
Test.prototype = new EventEmitter;
var Tester = new Test();
var Events = {
init: [],
event1:[],
event2:[]
};
var module;
for (module in modules)
{
var event;
for (event in Events)
if (typeof(modules[module][event]) === 'function')
Events[event].push(module);
}
var callModule = function(action){
Events[action].forEach(function(module){
modules[module][action]();
});
};
Tester.on('init', function(){callModule('init');});
Tester.on('event1', function(){callModule('event1');});
Tester.on('event2', function(){callModule('event2');});
console.time('Test #3');
Tester.emit('init');
for (var i = 0; i < maxIteration; ++i)
{
Tester.emit('event1');
Tester.emit('event2');
}
console.timeEnd('Test #3');
}
function Test4()
{
var Test = function(){
};
Test.prototype = new EventEmitter;
var Tester = new Test();
var Events = {
init: [],
event1:[],
event2:[]
};
var module;
for (module in modules)
{
var event;
for (event in Events)
if (typeof(modules[module][event]) === 'function')
Events[event].push(module);
}
var callModule = function(action){
async.each(Events[action], (function(module){
modules[module][action]();
}));
};
Tester.on('init', function(){callModule('init');});
Tester.on('event1', function(){callModule('event1');});
Tester.on('event2', function(){callModule('event2');});
console.time('Test #4');
Tester.emit('init');
for (var i = 0; i < maxIteration; ++i)
{
Tester.emit('event1');
Tester.emit('event2');
}
console.timeEnd('Test #4');
}
Test1();
Test2();
Test3();
Test4();
// |
Partager