0

次のコードがあります。

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);
    }
}

2回の繰り返しをループするとブラウザーが3〜4秒間ハングアップするときに、push()またはunshift()を空の配列に入れる理由がわかりません。

この行を省略した場合:

select_ids.unshift(element);

スクリプトは即座に実行されるため、

質問:
空の配列へのプッシュ/シフト解除でこのような遅延が発生するのはなぜですか? 参考までに、これは 3 回反復する別のループ内にあります。親ループごとに、select_ids を空のオブジェクトにリセットして埋めています。

ありがとう!

編集:
そして、配列に double を追加しないように if-clause を実行しています。

編集:(完全なコード)

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;
  };
4

1 に答える 1

0

OPから要求されたように、問題は実際には次のunshiftとおりです。過去にこの関数にバグがあり(そのコンテキストはアドホックです)、このバグレポートはまだ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

余談ですが、プロトタイプの楽しみ :)

于 2013-01-30T12:07:08.977 に答える