简体   繁体   English

如何优化JS代码?

[英]How to optimize JS code?

Chrome profiling say: "Not optimized: assignment to parameter in arguments object". Chrome浏览器性能分析说:“未优化:参数对象中参数的分配”。 What can i do for optimize this code? 我该怎么做才能优化此代码?

this.buffer.forEach(function(tilepos, ypos)
{
  tilepos.forEach(function(tileinfo, xpos)
  {
    _self.tiles.putTile('ground', xpos, ypos, _self.ground);
  });
});

It likely does not like that you are acting on tilepos within the block. 您可能不喜欢您对块内的tilepos进行操作。

I would recommend doing it as follows if you wish to eliminate the error, but also get a bit of a performance boost: 如果您想消除该错误,建议您按照以下步骤进行操作,但同时也可以提高性能:

for(var i = 0; i < this.buffer.length; i++)
{
    for(var j = 0; j < this.buffer[i].length; j++)
    {
        _self.tiles.putTile('ground', i, j, _self.ground);
    }
}

You are most likely overwriting the arguments by reassigning a function parameter or an arguments element. 您很可能通过重新分配函数参数或arguments元素来覆盖参数。

function f(a) {
    a = 100; // reassigns arguments[0]
};

Or 要么

function f(a) {
    arguments[0] = 100; // same thing
};

Your code looks fine to me, so I assume the problem lies in your putTile method. 您的代码对我来说看起来不错,所以我认为问题出在您的putTile方法中。 If you need to reassign the function parameters, create a local copy of it: 如果需要重新分配功能参数,请为其创建一个本地副本:

function f(a) {
    var localA = a;
    localA = 100;
}

If you provide the putTile method then I can take a look and update my answer. 如果您提供putTile方法,那么我可以看一下并更新我的答案。

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

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