Skip to content

Commit

Permalink
Use queueMicrotask for fast_next_tick in Web Worker environment. (c…
Browse files Browse the repository at this point in the history
…opy#477)

Optimize performance in worker.
  • Loading branch information
losfair authored Jun 5, 2021
1 parent 4e5f62b commit 8eb8dd9
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,24 @@ v86.prototype.init = function(settings)
this.bus.send("emulator-ready");
};

if(typeof setImmediate !== "undefined")

if(typeof importScripts === "function" && typeof queueMicrotask === "function")
{
let tick_counter = 0;

/** @this {v86} */
var fast_next_tick = function()
{
setImmediate(() => { this.do_tick(); });
if(tick_counter === 256)
{
tick_counter = 0;
setTimeout(() => { this.do_tick(); }, 0);
}
else
{
tick_counter++;
queueMicrotask(() => { this.do_tick(); });
}
};

/** @this {v86} */
Expand All @@ -96,6 +108,20 @@ if(typeof setImmediate !== "undefined")
/** @this {v86} */
var unregister_tick = function() {};
}
else if(typeof setImmediate !== "undefined")
{
/** @this {v86} */
fast_next_tick = function()
{
setImmediate(() => { this.do_tick(); });
};

/** @this {v86} */
register_tick = function() {};

/** @this {v86} */
unregister_tick = function() {};
}
else if(typeof window !== "undefined" && typeof postMessage !== "undefined")
{
// setImmediate shim for the browser.
Expand Down

0 comments on commit 8eb8dd9

Please sign in to comment.