javascript/jQuery:搜索和更新对象数组的最有效方法

javascript/jQuery:搜索和更新对象数组的最有效方法,javascript,jquery,arrays,json,performance,Javascript,Jquery,Arrays,Json,Performance,我目前正在从事一个私人领域/娱乐项目,这是一个基于javascript和jQuery的小型浏览器游戏。 对于这个游戏,我将数据保存到一个对象数组中: this.map.push( { "id" : id, "x" : pos_x, "y" : pos_y } ); 现在,我需要非常频繁地(以“实时”方式)查找和/或更新此数组中的数据。 例如,当我需要在coords系统的某个位置查找数组索引时,我正在使用jQuery的$。e

我目前正在从事一个私人领域/娱乐项目,这是一个基于
javascript
jQuery
的小型浏览器游戏。

对于这个游戏,我将数据保存到一个对象数组中:

this.map.push(
    { 
        "id" : id, 
        "x" : pos_x,
        "y" : pos_y
    }
);
现在,我需要非常频繁地(以“实时”方式)查找和/或更新此数组中的数据。

例如,当我需要在coords系统的某个位置查找数组索引时,我正在使用
jQuery
$。each()
函数:

this.getIndexAtPos = function(x, y)
{
    var index = false;
    $.each(this.map, function(key, obj){
        if(obj.x == x && obj.y == y)
        {
            index = key;
            return false;
        }
    });
    return index;
}
this.updateCoordsById = function(id, x, y)
{
    this.map = $.map(this.map, function(obj, i)
    {
        if(obj.id == id)
        {
            //only update x, y!
            obj.x = x;
            obj.y = y;
            return obj;
        }
        else
        {
            return obj;
        }
    });
}

另一个例子是:当我需要更新具有特定id的对象的数据时,我使用的是
jQuery
$.map()
函数:

this.getIndexAtPos = function(x, y)
{
    var index = false;
    $.each(this.map, function(key, obj){
        if(obj.x == x && obj.y == y)
        {
            index = key;
            return false;
        }
    });
    return index;
}
this.updateCoordsById = function(id, x, y)
{
    this.map = $.map(this.map, function(obj, i)
    {
        if(obj.id == id)
        {
            //only update x, y!
            obj.x = x;
            obj.y = y;
            return obj;
        }
        else
        {
            return obj;
        }
    });
}
到目前为止,这一切对我来说都很好,但我的电脑速度相当快,随着游戏的扩展,这些动作会越来越多。比如说每秒有几百个
$。每个
$。map
调用。
这就是为什么我担心性能问题,特别是在速度较慢的计算机上


我的问题: 我知道,
$。每个
$.map
在每次调用时都(部分或全部)迭代我的数组。这就是为什么我使用
returnfalse在我的
$中。一旦找到条目,每个
功能都会加快速度。

1.我可以通过使用其他
jQuery
函数或更改使用它们的方式来提高性能吗?
2.是否有更好的方法(性能方面)通过使用本机
javascript
解决此问题?
3.我应该使用其他数据类型/结构来提高性能吗

注意:我需要实现的过程总是非常相似的:通过coords(x,y)查找数组中的对象;通过id查找它们;通过坐标(x,y)更新对象;按id更新对象,依此类推



我将感谢任何帮助、意见和建议

好的,因为这是一个网格,所以将其作为网格存储在内存中是有意义的。 二维数组的访问速度比一维数组快得多,并且还允许您直接通过对象的坐标来访问对象,而不是检查每个对象的坐标

您还可以拥有一个容器对象,其中包含所有对象,并将其ID作为属性,这样可以根据ID进行快速查找

您可以通过将对象的ID存储在网格中,然后根据ID在容器中查找对象来将这些内容放在一起

我制作了一把小提琴,上面展示了一个简单的例子。地图对象的结构如下所示:

var Map = {
    init: function(){
        this.Width = 10;
        this.Height = 10;

        // grid stores the coordinates and the IDs of the object at that coordinate
        // note this is stored by y then x.
        this.grid = [];
        // stored the actual objects, indexed by id
        this.objects = {};

        // set up the grid        
        for(var y = 0; y < this.Height; ++y)
        {
            this.grid.push(new Array(this.Width));
        }    
    },

    // gets the object at (x, y)
    GetAtCoord: function(x, y)
    {
        // find the id
        var id = this.grid[y][x];
        // directly access it from the array
        return this.objects[id];
    },

    // gets the object with the ID
    GetById: function(objId)
    {
        // direct access to the object
        return this.objects[objId];
    },

    // add an object at its stored coordinates
    AddObject: function(obj){
        this.grid[obj.y][obj.x] = obj.id;
        this.objects[obj.id] = obj;
    },

    // Move an object in the grid
    MoveObject: function(objId, newX, newY)
    {
        // get the objct to move
        var theObj = this.objects[objId];
        // reove it from the grid
        this.grid[theObj.y][theObj.x] = null;
        // updates its stored position
        theObj.x = newX;
        theObj.y = newY;

        // add it back to the grid
        this.grid[newY][newX] = objId;
    }
};
var映射={
init:function(){
这个。宽度=10;
这个。高度=10;
//网格存储该坐标处对象的坐标和ID
//注意,这是由y然后x存储的。
this.grid=[];
//存储实际对象,按id索引
this.objects={};
//设置网格
对于(变量y=0;y
我认为这更多地体现在数据结构上,而不是访问数据所使用的函数上。我假设你的地图是(希望是矩形的)I宽j高的网格,并且在任何时刻(x,y)只能有一个对象,对吗?@Rhumborl是的,没错。1-no.2-是(原生JS总是比jQuery快)。3-是的,当然。你能想出任何答案吗?@Bergi我喜欢“否/是”的答案,它们非常有建设性。是的,一个用于高效逐位查找的二维对象数组和一个将id映射到用于高效逐位查找的对象会做得更好。试试看!哇,非常感谢你的努力我试试这个。另外,我还想知道
-1
,这取决于您希望如何在实际对象中存储坐标。数组从0开始,但某些坐标系从1开始,因此必须进行调整。如果您可以在对象中基于0存储它们,那么只需取出-1。刚刚找到您的
-1
的原因(var y=0;y
。如果您将其更改为
y++
,则不需要
-1
@Rhumborl好吧,除了MS Access中的数组之外,几乎所有内容都从
0
开始:dan代码的另一个问题:我想没有办法以这种方式继承或扩展
映射
,或者是吗?