简体   繁体   English

javascript拼接导致画布滞后

[英]javascript splice causing lag in canvas

I have a simple running game where platforms scroll from right to left. 我有一个简单的跑步游戏,平台从右向左滚动。 These platforms are stored in an array, and when they are off screen I use array.splice(index, 1) to remove it. 这些平台存储在数组中,当它们不在屏幕上时,我使用array.splice(index,1)删除它。 This however is causing an ever so slight lag on the exact second splice is called. 但是,这会导致在精确调用第二个接头时出现如此微小的滞后。 I have also used Array.shift() but I can still see the slight dip in frame rate as this is called. 我还使用了Array.shift(),但仍可以看到帧速率略有下降,这被称为。 Is there a better way to remove/destroy something? 有没有更好的方法来删除/销毁某些东西?

for(var x = 0; x < platforms.length; x++){
    var platform = platforms[x];

    platform.x -= 10;

    if(platform.x + platform.width < 0){
        platforms.shift();
    }
}

You could just not remove items from the array. 您无法从阵列中删除项目。

Use a fixed size array instead (large enough to store all the items you need) and flag as canceled the items you don't want to render anymore, you could reuse those flagged slots later. 改用固定大小的数组(足够大以存储所需的所有项目),并将不再需要渲染的项目标记为已取消,以后可以重用这些标记的插槽。

Or you could directly overwrite the elements if the domain of your problem allows it. 或者,如果问题所在的域允许,则可以直接覆盖这些元素。

[EDIT] [编辑]

Some more considerations in response to the comments: 针对评论的更多注意事项:

The evaluations of additional flag are computations stable in time, meaning that you can foresee how much time they will need and see if they fit the frame to render at a certain frame rate. 对附加标志的评估是时间上稳定的计算,这意味着您可以预见它们将需要多少时间,并查看它们是否适合以特定帧速率渲染的帧。 Array.splice on the other hand could trigger garbage collection and that could be some order of magnitude longer than other language control flow constructs. 另一方面, Array.splice可能会触发垃圾回收 ,并且可能比其他语言控制流构造长几个数量级

Garbage collection is an intensive task and should be avoided at all costs in the main loop of a game to achieve a fluid framerate. 垃圾收集是一项繁重的任务,应在游戏的主循环中不惜一切代价避免垃圾收集 ,以实现流畅的帧速率。 There are some resources and other questions here on SO which elaborate on this point, for example: http://buildnewgames.com/garbage-collector-friendly-code/ 关于SO,这里有一些资源和其他问题,例如: http : //buildnewgames.com/garbage-collector-friendly-code/

shift and splice are "slow" functions. shift和拼接是“慢速”功能。 They possibly rebuild your whole array. 他们可能会重建您的整个阵列。

Imagine having an area with 1000 items. 想象一下,一个拥有1000件物品的区域。 A shift could possibly be 'create a new array with all items, except the first'. 一个转变可能是“用除第一项外的所有项创建一个新数组”。 If your first 100 items now result in a shift, you rebuild 100 arrays, with 900-1000 items, which will result in about 100.000 inserts, 100 new array allocations. 如果您现在的前100个项目导致转移,您将重建100个数组,其中900-1000个项目,这将导致大约100.000插入,以及100个新的数组分配。

for(var i = 0; i < array.length; i++)
{
    if (array[i] == ....)
    {
        var newArray = new Array(array.length - 1);
        for(var o = 1; o < array.length; o++)
            newArray[o - 1] = array[o];
        array = newArray
    }
}

worst case scenario, with a length of 1000, this will result in: 最坏的情况是,长度为1000,将导致:

for ( i = 0 to 1000 )
    for ( o = i to 1000 )
        recreate the array

so that would be 0.5million iterations and recreations of the array. 因此这将是数组的50万次迭代和重新创建。 While this could be fixed with either 1 iteration (dynamicly sized array) or with 2 passes (fixed sized array): 虽然可以通过1次迭代(动态大小的数组)或2次遍历(固定大小的数组)来解决:

// dynamic size
var newArray = new Array();
for(var i = 0; i < array.length; i++)
{
    if (array[i] != ....)
        newArray.push(array[i]);
}
array = newArray;

// fixed size 
var cnt = 0;
for(var i = 0; i < array.length; i++)
    if (array[i] != ....)
        cnt++;
var newArray = new Array(cnt);
for(var i = 0, o = 0; i < array.length; i++)
    if (array[i] != ....)
        newArray[o++] = array[i];
array = newArray;

and another simple optimization for your for loops (which obviously wont work if you modify the array in the for loop): 和for循环的另一个简单优化(如果在for循环中修改数组,则显然不起作用):

for(var i = 0, l = array.length; i < l; i++)

(yes, i am aware that some numbers may be off. but it gets the point across.) (是的,我知道有些数字可能会减少。但这很清楚。)

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

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