html - 如何测试 javascript 代码的速度 - SVG 与 CANVAS

标签 html performance testing canvas svg

我有两个类似的示例,但一个使用 SVG,另一个使用 CANVAS:

使用 html5 Canvas :http://jsbin.com/yepigu/5 使用 SVG:http://jsbin.com/yumova

我需要知道哪个渲染速度更快。我需要在我的案例中使用什么? 是否有一些在线工具,或者我需要将一些计时器写入代码?

最佳答案

要对 2 个备选方案进行性能测试,请在记录开始和结束时间的同时运行每个备选方案。

顺便说一句,我修改了你的 Canvas 描边线,使其在没有阴影的情况下工作,这应该会加快速度:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;


var pts = [{x:22,y:59.45},{x:136,y:66},{x:170,y:99},{x:171,y:114},{x:183,y:125},{x:218,y:144},{x:218,y:165},{x:226,y:193},{x:254,y:195},{x:283,y:195},{x:292,y:202},{x:325,y:213},{x:341,y:134},{x:397,y:245},{x:417,y:548}];


ctx.lineCap='round';
ctx.lineJoin='round';
ctx.lineWidth=25;
ctx.strokeStyle='red';
drawPolyline(pts);
ctx.lineWidth=22;
ctx.strokeStyle='pink';
drawPolyline(pts);
ctx.lineWidth=2;
ctx.strokeStyle='blue';
drawPolyline(pts);

function drawPolyline(pts){
  ctx.beginPath();
  ctx.moveTo(pts[0].x,pts[0].y);
  for(var i=1;i<pts.length;i++){
    ctx.lineTo(pts[i].x,pts[i].y);
  }
  ctx.stroke();
}
body{ background-color: ivory; padding:10px; }
#canvas{border:1px solid red;}
<canvas id="canvas" width=600 height=600></canvas>

关于html - 如何测试 javascript 代码的速度 - SVG 与 CANVAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27994381/

相关文章:

javascript - 如何在 vue bootstrap 中启用悬停下拉菜单?

html - 在 HTML 中对密码添加限制?

python - 如何提高数百个文件中数千行的解析效率

c++ - 是否有关于使用 C++ 编译器进行 C 编译的结论性研究/实验?

java - 循环两次但在 JAVA 中花费相同的时间

ruby-on-rails - rails 5.1.4 assert_select 'li' ,仅检查第一个 li

Django 测试。运行测试时从生产数据库查找数据?

html - 选项卡式分页帮助

windows - 如何限制 GPU 和 CPU 的使用以进行兼容性测试?

php - 图像在 mysql 中无法正确显示