var img = new window.Image()
var imageBitmap
const canvas = document.createElement('canvas')
canvas.width = 640
canvas.height = 480
var ctx = canvas.getContext('2d')
var promise = new Promise(resolve => {
img.addEventListener('load', function() {
window.createImageBitmap(img)
.then(function(bitmap) {
imageBitmap = bitmap
resolve()
})
})
})
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg';
promise.then(()=> ctx.drawImage(img, 0, 0));
promise.then(()=> ctx.drawImage(imageBitmap, 0, 0));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
Bitmap |
Test name | Executions per second |
---|---|
Image | 1246766.1 Ops/sec |
Bitmap | 1316210.0 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test compares two approaches: drawing an image using the drawImage
method and converting it to a bitmap using the Web API's createImageBitmap
function.
Options Compared
The benchmark tests two options:
drawImage
method to render the image on the canvas.createImageBitmap
function and then drawing it on the canvas.Pros and Cons of Each Approach
Drawing the Image Directly
Pros:
Cons:
Converting the Image to a Bitmap
Pros:
Cons:
createImageBitmap
)Library Used
The benchmark uses the Web API's createImageBitmap
function from the window.createImageBitmap()
method. This function is used to create a bitmap representation of an image, which can then be drawn on the canvas.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this benchmark that would require specific knowledge or expertise.
Other Alternatives
Other alternatives for drawing images on the web include:
drawImage
method with other optimizations, such as caching images or using texture atlasingIn conclusion, this benchmark provides a clear comparison between two approaches to drawing images on the web. The choice of approach depends on the specific use case and performance requirements.