English 中文(简体)
改进分类的绩效
原标题:improving the performance of sorting
var sorted = results.sort(function (a, b) {
    var nameA = "";
    var nameB = "";
    var str1 = a.date.toLowerCase() + " " + a.time;
    var str2 = b.date.toLowerCase() + " " + b.time;

    if (a.date.toLowerCase() != "" && a.time!= "") 
     nameA = Date.parse(new Date(str1))

    if (b.date.toLowerCase() != "" && b.time!= "") 
     nameB = Date.parse(new Date(str2))

    return nameA === nameB ? 0 : nameA < nameB ? -1 : 1;

});
  1. Can anyone improve the code, if there any faults in the way i am sorting. Is there any better approach?
  2. How can i implement http://underscorejs.org/#sortBy for the above sorting.
最佳回答

您代码中的一个主要瓶颈是您在每种操作中重新对名称 A/ nameB 进行重新比较。 考虑到数组中的每个元素都可能参加多个比较, 这会有很多不必要的工作。 您应该首先使用相同的算法构建排序索引, 然后用它进行排序。 您也可以将排序键存储在同一对象中, 这样您就可以检查它是否在这里, 如果是的话, 不要重新计算它 。

您的代码中另有两件事是绝对不必要的: .to LowerCase 与空字符串进行比较,并生成无用的 Data 对象,因为 Data.parse 已经明确指定为用字符串工作。

我制作了两种改良的版本——一种是辅助功能(更精细,更正确的意识形态)和内线——可以对某些联署材料引擎提供一些额外的加速。两者的运行速度都比原来的速度快大约3000倍:

内含 :

var sorted = results.sort(function (a, b) {
    var nameA = a.dateTimeMs
    var nameB = b.dateTimeMs
    if (!nameA && nameA !== "" && !nameA !== 0){
        if (a.date == "" || a.time == ""){
            nameA = a.dateTimeMs = ""
        } else {
            nameA = a.dateTimeMs = Date.parse(a.date.toLowerCase() + " " + a.time)
        }
    }
    if (!nameB && nameB !== "" && !nameB !== 0){
        if (b.date == "" || b.time == ""){
            nameB = b.dateTimeMs = ""
        } else {
            nameB = b.dateTimeMs = Date.parse(b.date.toLowerCase() + " " + b.time)
        }
    }

    return nameA === nameB ? 0 : nameA < nameB ? -1 : 1;
});
问题回答

最好的办法是让您的 a 和 b 对象从一开始就计算出来。 而不是在无需处理的情况下循环。 或者按照 Oleg 的存储方式来保存它 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签