次のコードがあります。
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;
};