<img id="source" src="https://placekitten.com/1280/720" crossorigin="anonymous">
// const sourceImage = new Image();
const sourceImage = document.querySelector('#source');
const imageLoaded = new Promise((resolve, reject) => {
if (sourceImage.complete) resolve(sourceImage);
sourceImage.onload = () => resolve(sourceImage);
sourceImage.onerror = reject;
// resolve(source1);
});
const imageBlobLoaded = fetch('https://placekitten.com/1280/720').then(res => res.blob())
function scaleSource(sw, sh, dw, dh) {
const hRatio = dw / sw;
const vRatio = dh / sh;
const ratio = Math.max(hRatio, vRatio);
const x = (sw - dw / ratio) / 2;
const y = (sh - dh / ratio) / 2;
return { x, y, w: sw - x * 2, h: sh - y * 2, ratio };
}
const profileWidth = 480;
const profileHeight = 360;
// Remember, all the outputs must be an ImageBitmap to be a fair comparison
// case 1: use createImageBitmap to crop and resize, bugged in firefox but working in Chrome
async function case1_createImageBitmapWithImg() {
const source = await imageLoaded;
const bitmap = await window.createImageBitmap(
source,
source.width,
source.height,
0,
0
);
return bitmap;
}
// case 2: use canvas to crop and resize
async function case2_createImageBitmapWithBlob() {
const source = await imageBlobLoaded;
const bitmap = await window.createImageBitmap(
source,
source.width,
source.height,
0,
0
);
return bitmap;
}
case1_createImageBitmapWithImg()
.then(bitmap => bitmap.close());
case2_createImageBitmapWithBlob()
.then(bitmap => bitmap.close());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Create ImageBitmap with Img element | |
Create ImageBitmap with Blob |
Test name | Executions per second |
---|---|
Create ImageBitmap with Img element | 2451916.5 Ops/sec |
Create ImageBitmap with Blob | 1893041.2 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark that tests two approaches to create an ImageBitmap
object:
<img>
element as the source image.Options Compared
The benchmark compares the performance of two options:
<img>
element as the source image (case1_createImageBitmapWithImg
)case2_createImageBitmapWithBlob
)Pros and Cons of Each Approach
<img>
element and directly uses the blob data.Library Used
In both cases, the benchmark uses the fetch
API to load the blob from a URL. The fetch
API is a modern JavaScript library that allows for making HTTP requests and working with response data in a more convenient way.
Special JS Feature or Syntax
The benchmark does not explicitly use any special JavaScript features or syntax beyond what's considered standard (ES6+). However, it relies on the async/await
syntax to write asynchronous code in a synchronous-looking manner.
Other Considerations
When writing microbenchmarks like this one, consider the following:
Alternatives
If you were to rewrite this benchmark using other alternatives, here are some options:
<img>
element from a URL, consider pre-loading the image data directly into memory.