最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

performance - Javascript foreach with condition - Stack Overflow

programmeradmin4浏览0评论

Here you can see my code :

this.tiles.forEach ( function($tile)
    {
            $tile.content.x = ( $tile.posY - $tile.posX ) * ($tile.map.tilesWidth/2) + ($tile.offsetX + $tile.map.offsetX);
            $tile.content.y = ( $tile.posY + $tile.posX ) * ($tile.map.tilesHeight/2) + ($tile.offsetY + $tile.map.offsetY);

            $tile.content.tile = $tile;
    });

So, for each tile in my array tiles i do some calculs.

Each item in my array have an attribut posX and posY.

My probleme here it's if i have a lots of tiles in my array, this foreach take a long time to execute.

I need to add a condition and do this stuff for each tile where posX is between Xmin and Xmax, same thing for posY.

How can i do that as simply as possible? To save the greatest possible resource.. thanks !

Add a if condition in my array is not a good solution cause the foreach will still go through the whole array..

Here you can see my code :

this.tiles.forEach ( function($tile)
    {
            $tile.content.x = ( $tile.posY - $tile.posX ) * ($tile.map.tilesWidth/2) + ($tile.offsetX + $tile.map.offsetX);
            $tile.content.y = ( $tile.posY + $tile.posX ) * ($tile.map.tilesHeight/2) + ($tile.offsetY + $tile.map.offsetY);

            $tile.content.tile = $tile;
    });

So, for each tile in my array tiles i do some calculs.

Each item in my array have an attribut posX and posY.

My probleme here it's if i have a lots of tiles in my array, this foreach take a long time to execute.

I need to add a condition and do this stuff for each tile where posX is between Xmin and Xmax, same thing for posY.

How can i do that as simply as possible? To save the greatest possible resource.. thanks !

Add a if condition in my array is not a good solution cause the foreach will still go through the whole array..

Share Improve this question asked Nov 17, 2013 at 15:31 Clément AndraudClément Andraud 9,26927 gold badges86 silver badges160 bronze badges 2
  • forEach goes through the entire array, no matter what. Try adding an if expression around the 3 lines of code inside that function that checks for your desired range in $tile.posX – Joe Simmons Commented Nov 17, 2013 at 15:34
  • Can you give an example of your array? – David Thomas Commented Nov 17, 2013 at 15:47
Add a ment  | 

1 Answer 1

Reset to default 12

You could use the filter method:

this.tiles
    .filter ( function($tile)
    {
            return $tile.posX <= Xmin && $tile.posX >= Xmax && 
                   $tile.posY <= Ymin && $tile.posY >= Ymax;
    })
    .forEach ( function($tile)
    {
            $tile.content.x = ( $tile.posY - $tile.posX ) * ($tile.map.tilesWidth/2) + ($tile.offsetX + $tile.map.offsetX);
            $tile.content.y = ( $tile.posY + $tile.posX ) * ($tile.map.tilesHeight/2) + ($tile.offsetY + $tile.map.offsetY);

            $tile.content.tile = $tile;
    });

But a simple for-loop would be more efficient:

for (var i = 0; i < this.tiles.length; i++)
{
    var $tile = this.tiles[i];
    if ($tile.posX <= Xmin && $tile.posX >= Xmax && 
        $tile.posY <= Ymin && $tile.posY >= Ymax)
    {
        $tile.content.x = ( $tile.posY - $tile.posX ) * ($tile.map.tilesWidth/2) + ($tile.offsetX + $tile.map.offsetX);
        $tile.content.y = ( $tile.posY + $tile.posX ) * ($tile.map.tilesHeight/2) + ($tile.offsetY + $tile.map.offsetY);

        $tile.content.tile = $tile;
    }
}
发布评论

评论列表(0)

  1. 暂无评论