为什么在Firefox中将元素推入空对象中?

时间:2013-01-30 11:48:35

标签: javascript jquery arrays firefox loops

我有以下代码:

var select_ids = [];
// syntax.filter.select_list is ["elem1", "elem2"]
for (var o = 0; o < syntax.filter.select_list.length; o += 1) {
    element = syntax.filter.select_list[o];
    if (priv.getPositionInArray(element,select_ids) === null ) {
      // this line hangs up the browser
      select_ids.unshift(element);
    }
}

我不明白为什么push()或unshift()进入一个空数组时循环两次迭代会挂起浏览器3-4秒。

如果我离开这一行:

select_ids.unshift(element);

脚本立即运行,所以

问题:
为什么可以推送/取消移动到空数组产生这样的延迟?仅供参考,这是另一个循环,有3次迭代。对于每个父循环,我将select_ids重置为要填充的空对象。

谢谢!

修改
我正在做if子句以确保我不向数组添加双精度。

编辑:(完整代码)

priv.findBestIndexForQuery = function (indices, syntax) {
    var i, j, k, l, m, n, p, o, element,
      search_ids = [], select_ids = [], use_index = [],
      index, query_param, search_param,
      // need to parse into object
      current_query = jIO.ComplexQueries.parse(syntax.query);

    // loop indices
    for (i = 0; i < priv.indices.length; i += 1) {
      index = {};
      index.reference = priv.indices[i];
      index.reference_size = index.reference.fields.length;

      // rebuild for iteration
      if (current_query.query_list === undefined) {
        search_ids.push(current_query.id);
      } else {
        for (j = 0; j < current_query.query_list.length; j += 1) {
            if (priv.getPositionInArray(current_query.query_list[j].id, 
                search_ids) === null ) {
                search_ids.push(current_query.query_list[j].id);
            }
        }
      }
      for (o = 0; o < syntax.filter.select_list.length; o += 1) {
         element = syntax.filter.select_list[o];
         if (priv.getPositionInArray(element,select_ids) === null ) {
              // line causing problems
              select_ids.unshift(element);
         }
      }
// there is a lot more, but I'm hanging on the line above
}

编辑(getPostionInArray)

  priv.getPositionInArray = function (element, array) {
     var i;
     for (i = 0; i < array.length; i += 1) {
        if (array[i] === element) {
           return i;
        }
     }
     return null;
  };

1 个答案:

答案 0 :(得分:0)

根据OP的要求,问题确实是unshift:过去有这个函数的漏洞(它的上下文是临时的),this bug report仍然标记为< EM> NEW

至于您的getPositionInArray功能,为什么不使用标准indexOf方法?大多数浏览器都支持它,对于那些不支持的浏览器,您可以随时将其添加到脚本中:

if (!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function(search)
    {
        var i;
        for (i = 0; i < this.length; i++)
        {
            if (this[i] === search)
            {//I use type & value check...
                return i;
            }
        }
        return -1;//-1 is the standard behaviour
    };
}

或者,如果您希望此方法在所有浏览器中返回null,而不是-1

Array.prototype.indexOf = (function (nativeFunc)
{
    return function(search)
    {
        var retVal = nativeFunc.apply(this,[search]);//use native method
        return (retVal === -1 ? null : retVal);//return null if the native method returned -1
    };
}(Array.prototype.indexOf));//pass initial method to IIFE

只是旁注,一些原型乐趣:)