var canvas = document.createElement('canvas')
var context = canvas.getContext('2d')
var bitmap
var img = new Image()
img.onload = async () => {
canvas.width = img.width
canvas.height = img.height
context.drawImage(img, 0, 0)
bitmap = await createImageBitmap(img)
}
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg'
var c = document.createElement('canvas').getContext('2d')
// bitmap && c.drawImage(img, 250, 250, 250, 250, 0, 0, 1000, 1000)
// bitmap && c.drawImage(canvas, 250, 250, 250, 250, 0, 0, 1000, 1000)
// bitmap && c.drawImage(bitmap, 250, 250, 250, 250, 0, 0, 1000, 1000)
bitmap
bitmap && c.drawImage(canvas, 250, 250, 250, 250, 0, 0, 1000, 1000)
bitmap && c.drawImage(bitmap, 250, 250, 250, 250, 0, 0, 1000, 1000)
bitmap && c.drawImage(img, 250, 250, 250, 250, 0, 0, 1000, 1000)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
empty | |
canvas | |
bitmap | |
image |
Test name | Executions per second |
---|---|
empty | 24984804.0 Ops/sec |
canvas | 477218.4 Ops/sec |
bitmap | 378751.3 Ops/sec |
image | 967282.4 Ops/sec |
Let's dive into explaining the benchmark test case on MeasureThat.net.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark that tests the performance of drawing an image using different approaches. The benchmark is named "drawImage() img vs canvas vs bitmap + cropping, fix loading".
Options Compared
There are four options compared in this benchmark:
createImageBitmap
API to create a bitmap representation of the image.<canvas>
element and its 2D drawing context (context
) to draw the image directly onto the canvas.createImageBitmap
, and then drawing that bitmap onto a separate canvas.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
createImageBitmap
API.Library Usage
The benchmark uses the createImageBitmap
API, which is a part of the Web API specification. This library provides an efficient way to create bitmap representations of images.
Special JS Features/Syntax
There are no special JavaScript features or syntax used in this benchmark that would require additional explanation.
Other Considerations
When choosing between these approaches, consider the following factors:
createImageBitmap
API or combining both bitmap and canvas approaches might be more efficient.Alternatives
Other alternatives for drawing images in JavaScript include:
<img>
element directly, which can provide a simple way to display images without requiring additional library usage.I hope this explanation helps you understand the benchmark test case on MeasureThat.net!