简体   繁体   English

HTML5 drawImage:Chrome 比 Mozilla 慢 9 倍

[英]HTML5 drawImage: Chrome is nine times slower than Mozilla

Why is the code on Chrome nine times slower than on Mozilla?为什么 Chrome 上的代码比 Mozilla 慢九倍? Can I increase the speed of Chrome?我可以提高 Chrome 的速度吗? On my computer, Chrome performs 9,302 ops/s, and Mozilla 86,715 ops/s, and on my laptop the difference is 20 times.在我的电脑上,Chrome 执行 9,302 ops/s,Mozilla 执行 86,715 ops/s,而在我的笔记本电脑上,差异是 20 倍。 The Edge browser is also faster than chrome five times. Edge 浏览器也比 Chrome 快五倍。 Test link https://jsbench.me/nuk5skh6qi/1测试链接https://jsbench.me/nuk5skh6qi/1

<canvas id="src" width="100" height="100"></canvas>
<canvas id="mask" width="100" height="100"></canvas>
<canvas id="dest" width="100" height="100"></canvas>
var src = document.getElementById('src');
var srcCtx = src.getContext('2d');

var mask = document.getElementById('mask');
var maskCtx = mask.getContext('2d');

var dest = document.getElementById('dest');
var destCtx = dest.getContext('2d');

function test(sz) {
   srcCtx.drawImage(mask, 0, 0, sz, sz, 0, 0, sz, sz);
   destCtx.drawImage(src, 0, 0, sz, sz, 0, 0, sz, sz);
}

Check settings检查设置

I can not duplicate a 20 times slowdown between Chrome and FF on various desktops and laptops, average difference is Chrome about 20-30% the throughput of FF.我无法在各种台式机和笔记本电脑上复制 Chrome 和 FF 之间 20 倍的减速,平均差异是 Chrome 大约是 FF 吞吐量的 20-30%。 But then a year ago (about) they were even.但是一年前(大约)他们是平等的。 Not much you can do when most people use Chrome.当大多数人使用 Chrome 时,您无能为力。

Suggest that you make sure you don't have any problems with chrome and the GPU nav to建议你确保你的 chrome 和 GPU 导航没有任何问题

  • chrome://gpu it will list any problems with the GPU chrome://gpu它将列出 GPU 的任何问题
  • chrome://flags check you have "Accelerated 2D canvas" enabled chrome://flags检查您是否启用了“加速 2D 画布”

WebGL for performance性能的 WebGL

If you want good performance across devices and browsers you need to consider WebGL as it makes the 2D API snail like in comparison.如果您希望跨设备和浏览器获得良好的性能,则需要考虑 WebGL,因为相比之下,它使 2D API 变得像蜗牛一样。

Rendering bottle neck渲染瓶颈

Note that switching from one render destination to another can tax some hardware, nor would I recommend doing so as part of any rendering pipeline built on the 2D API.请注意,从一个渲染目标切换到另一个渲染目标可能会对某些硬件造成负担,我也不建议将其作为基于 2D API 构建的任何渲染管道的一部分。

If you need to render to a secondary canvas do it all in one go, then render that content to the other canvas.如果您需要渲染到辅助画布,一次性完成所有操作,然后将该内容渲染到另一个画布。

For example testA is ~10 times slower than testB on Firefox and Chrome.例如testA在 Firefox 和 Chrome 上比testB慢约 10 倍。 Both functions move the same number of pixels.这两个函数移动相同数量的像素。

   function testA(sz = 100) {
       var count = 100;
       while (count--) {
           srcCtx.drawImage(mask, 0, 0, sz, sz, 0, 0, sz, sz);
           destCtx.drawImage(src, 0, 0, sz, sz, 0, 0, sz, sz);
       }
   }

   function testB(sz = 100) {
       var count = 100;
       while (count--) {
           srcCtx.drawImage(mask, 0, 0, sz, sz, 0, 0, sz, sz);
       }
       count = 100;
       while (count--) {
           destCtx.drawImage(src, 0, 0, sz, sz, 0, 0, sz, sz);
       }
   }

Performance result for above functions上述功能的性能结果

Chrome Version 79.0.3945.130 Chrome 版本 79.0.3945.130

testA.: 21,386.106 ±802.033µs OPS    46  10% Total 22,242ms 1,040 operations
testB.:  2,120.172 ±50.374µs  OPS   471 100% Total  2,035ms   960 operations

Firefox 73.0b9火狐 73.0b9

testA.:  6,611.111 ±47.258µs  OPS   151  14% Total 19,635ms 2,970 operations
testB.:    907.921 ±31.091µs  OPS 1,101 100% Total  2,751ms 3,030 operations

OPS is Operations per second. OPS是每秒操作数。 An Operation is one call to the test function操作是对测试函数的一次调用

It is not possible to increase the speed of any browser.无法提高任何浏览器的速度。

The rendering speed is due to several factors such as the processing power of your PC (memory, CPU, etc.), the speed of your internet and finally the browser core.渲染速度取决于多种因素,例如 PC 的处理能力(内存、CPU 等)、互联网速度以及浏览器核心。

When performing the tests make sure that the environment is in the same scenario.执行测试时,请确保环境处于相同的场景中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM