Тесты на скорость выполнения:
var intervals = [], i = 100, t1 = Date.now(), t2;
setTimeout(function () {
t2 = Date.now();
intervals.push(t2 - t1);
t1 = t2;
if (i--) {
setTimeout(arguments.callee, 0);
}
else {
alert(intervals);
}
}, 0);
var message = "ServiceMessage", storage = {}, uid = 0;
addEventListener("message", function (event) {
var key = event.data, data;
if (typeof key == "string" && key.indexOf(message) == 0) {
data = storage[key];
data.callback.call(data.thisObj, data.data);
delete storage[key];
}
});
function callAsync(callback, thisObj, data) {
var key = message + uid++;
storage[key] = {
data: data,
thisObj: thisObj,
callback: callback
};
postMessage(key, "*");
}
var intervals = [], i = 100, t1 = Date.now(), t2;
callAsync(function () {
t2 = Date.now();
intervals.push(t2 - t1);
t1 = t2;
if (i--) {
callAsync(arguments.callee);
}
else {
alert(intervals);
}
});