English 中文(简体)
jquery .filter()在大型阵列中的功能缓慢
原标题:jquery .filter() function on large array is slow in IE

我有一阵.,含有约6300个元素。

我试图通过这些内容利用 j子,并为一些可以用来过滤6300个元素的下级清单制定备选办法清单。

在欧安办事处,这项工作没有问题,但在伊埃,我对手稿有误。 我在玩弄该法典,试图获得一套数据,而不造成IEE的错误,但迄今为止,我没有幸运。 以下是我已经尝试的方法:

利用“指标”编制一个单独的“管辖权清单”。

    var arrayJurisdiction = dataSet.filter(function (item, i, a) {
        return i.Jurisdiction == a.indexOf(item.Jurisdiction);
    });

核对每个要素,检查是否在二级阵列中已经存在价值,如果是没有,则添加。

    g$.each(dataSet, function (key, value) {
        var matchingJurisdiction = arrayJurisdiction.filter(function (item) {
            return value.Jurisdiction == item;
        })[0];
        if (matchingJurisdiction == null) {
            arrayJurisdiction.push(value.Jurisdiction);
        }
    });

Both of these methods result in IE giving me an error about a script running too slowly. Is there any faster way of doing this?

*EDIT*** Based on feedback below, I have changed the method to use for loops instead of .each() and .filter(), but I am still receiving the "stop this script?" dialog in IE.

这里是使用假体的经修订的法典。 此外,我还包括了我试图填满的所有过滤器(而不仅仅是第一个过滤器)。

    for (var i = 0; i < dataSet.length; i++) {
        var value = dataSet[i];

        var matchingJurisdiction = null;
        for (var i = 0; i < arrayJurisdiction.length; i++) {
            var item = arrayJurisdiction[i];
            if (item == value.Jurisdiction) {
                matchingJurisdiction == item;
                break;
            }
        }
        if (matchingJurisdiction == null) {
            arrayJurisdiction.push(value.Jurisdiction);
        }



        var valueYear = new Date(value.Treatment_Date).getFullYear();
        var matchingYear = null;
        for (var i = 0; i < arrayYear.length; i++) {
            var item = arrayYear[i];
            if (item == valueYear) {
                matchingYear == item;
                break;
            }
        }

        if (matchingYear == null) {
            arrayYear.push(valueYear);
        }
        var matchingProjectClass = null;
        for (var i = 0; i < arrayProjectClass.length; i++) {
            var item = arrayProjectClass[i];
            if (item == valueYear) {
                matchingProjectClass == item;
                break;
            }
        }
        if (matchingProjectClass == null) {
            arrayProjectClass.push(value.Project_Classification);
        }

        var matchingImprovementType = null;
        for (var i = 0; i < arrayImprovementType.length; i++) {
            var item = arrayImprovementType[i];
            if (item == valueYear) {
                matchingImprovementType == item;
                break;
            }
        }
        if (matchingImprovementType == null) {
            arrayImprovementType.push(value.Improvement_Type);
        }
    }
问题回答

Dropping jQuery each() is certainly an option as it does some funny stuff with the scope beside actual iteration. Another real is that your algorithm seem to be O(n^2) which is pretty bad. I would simply sort your dataSet on Jurisdiction value, and then loop over keeping previous value and eliminating duplicates. That is going to be the O of the sort algorithm, which is likely using quick sort algorithm O(n*log(n)), but is also likely implemented much more efficiently in native code.

dataSet.sort(function(a, b)
{
    if (a.Jurisdiction > b.Jurisdiction)
        return 1;
    if (a.Jurisdiction < b.Jurisdiction)
        return -1;
    return 0;
});

var prev = null;
var filtered = new Array(dataSet.length);
for (var i=0; i<dataSet.length; i++)
{
    if (prev == null || prev.Jurisdiction != dataSet[i].Jusrisdiction)
        filtered.push(dataSet[i]);
    prev = dataSet[i];
}




相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...