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.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 | 0.0 Ops/sec |
worker thread | 619289.3 Ops/sec |
Let's break down the provided JSON to understand what is being tested and compare different approaches.
Benchmark Overview
The benchmark compares the performance of two approaches:
ctx.drawImage
and then retrieving the image data with getImageData
.Test Cases
There are two test cases:
postMessage
.postMessage
with the imageData
buffer.postMessage
.imageData
buffer back to the main thread using postMessage
.Library Used
The script uses the following libraries:
CanvasRenderingContext2D
: used for drawing images on the canvas.Web Workers API
: used for sending data between threads.Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax that are specific to a particular browser or version. It focuses on the performance comparison of different approaches using standard Web APIs.
Pros and Cons of Different Approaches
Here's a brief summary of the pros and cons of each approach:
Other Alternatives
There are other approaches to compare the performance of drawing images on a canvas:
transferControlToOffscreen
to create an offscreen canvas, you could use the new OffscreenCanvas
API, which provides a more efficient and streamlined way to work with offscreen canvases.postMessage
, you could use shared arrays to store the image data in a thread-safe manner.However, these alternatives are not part of this specific benchmark and would require additional modifications to the script.
In summary, this benchmark compares the performance of two approaches: drawing an image on the main thread and then retrieving the image data, versus sending the image data from a worker thread to the main thread. The results highlight the benefits of using non-blocking approaches, such as Web Workers API, for computationally expensive tasks.