var blob = new Blob([
`
let bitmap = undefined;
let canvas = undefined;
let ctx = undefined;
let postAMessage=self.postMessage;
self.onmessage = function(ev) {
if(ev.data.msg === 'bitmap' && canvas != undefined) {
bitmap = ev.data.imageB;
console.log("Received an ImageBitmap next!");
while (true)
{
ctx.clearRect(0,0,640,480);
ctx.drawImage(bitmap, 0,0);
}
}
if(ev.data.msg === 'init' && bitmap != undefined) {
canvas = ev.data.canvas;
ctx = canvas.getContext('2d');
console.log("Received a canvas next!");
while (true)
{
ctx.clearRect(0,0,640,480);
ctx.drawImage(bitmap, 0,0);
}
}
if(ev.data.msg === 'bitmap' && canvas == undefined) {
console.log("Received a bitmap first!");
bitmap = ev.data.imageB;
}
if(ev.data.msg === 'init' && bitmap == undefined) {
console.log("Received a canvas first!");
canvas = ev.data.canvas;
ctx = canvas.getContext('2d');
}
}
`]);
var myWorker = new Worker(window.URL.createObjectURL(blob));
var buff = new ArrayBuffer(8);
var img = new Image();
img.crossOrigin = 'Anonymous';
img.src = "https://www.google.com/s2/favicons?domain=www.google.com";
img.decode().then(() => {createImageBitmap(img).then(Ibitmap => { myWorker.postMessage({imageB: Ibitmap, msg:'bitmap'}, [Ibitmap]); })});
var canvas = document.createElement("canvas");
var c2 = document.createElement("canvas");
c2.width = 640;
c2.height = 480;
c2 = c2.transferControlToOffscreen();
canvas.width = 640;
canvas.height = 480;
c2.width = 640;
c2.height = 480;
var ctx = canvas.getContext('2d');
myWorker.postMessage({msg: 'init', canvas: c2}, [c2]);
if (img.width > 0){
ctx.drawImage(img, 0, 0);
var imageData = ctx.getImageData(0, 0, img.width, img.height);
}
if (img.width > 0) myWorker.postMessage(buff);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Main Thread | |
Worker Thread |
Test name | Executions per second |
---|---|
Main Thread | 9284084.0 Ops/sec |
Worker Thread | 9628388.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark compares the performance of two approaches: drawing an image on the main thread (using Canvas) and sending an image to a worker thread for processing, which then draws the same image using OffscreenCanvas. The goal is to determine whether using a separate thread improves performance in this specific use case.
Test Cases
There are two test cases:
Options Compared
The benchmark compares two options:
Pros and Cons of Each Approach
Main Thread:
Pros:
Cons:
Worker Thread:
Pros:
Cons:
Other Considerations
Alternatives
If you're looking for alternatives to this benchmark or want to explore other approaches, here are some options:
Keep in mind that the choice of approach depends on your specific use case and requirements.