【问题标题】:Improve program performance [closed]提高程序性能[关闭]
【发布时间】:2012-11-15 14:44:46
【问题描述】:

我有一个 javascript 程序,它使用我的 JSON 数据 (200Mega) 执行操作 该程序通过 regxp 搜索我的数据中的出现

var myDatas = [
 { "id" : "0000000001",
   "title" :"Data1",
   "info": {
             "info1": "data data data",
             "info2": "infoinfoiinfoinfo",
             "info3": "info333333333",
             "miscellaneous": "",
             "other": [
                 {"title": "qwe", "ref": "other"},
                 {"title": "sdsd", "ref": "other"},
                 {"title": "ddd", "ref": "123"} 
             ]
           },
    "text": "xxxx text sldjdjskldj text text" },
 . . . ];

实际执行太慢了。

我会使用 html 5 的 Worker,但 IE9 不支持 IE9。

我的计划:

    iterObject : function(obj){

        var text = $('#search-input').val() //text to search

        var self = this
        $.each(obj, function(key, value) {
            if((typeof value) == 'object'){
                self.iterObject(value)
            }
            else{
                 self.Search(text, value)
              }
          }
      }

Search : function(item, text){

        var regexThisFragment =
                          new RegExp("[^a-zA-Z0-9]+.{0,40}[^a-zA-Z]+" + 
                          item + 
                          "[^a-zA-Z]+.{0,40}[^a-zA-Z0-9]+", "i")

        while(text.match(regexThisFragment) != null)
        {
            var fragment = text.match(regexThisFragment).toString()
            console.log(fragment );
            }
        }
},

如何改进我的程序?

【问题讨论】:

  • 另外,它提取了什么? myDatas 是完整的 200 MB 吗?或者您想在 200 MB 中找到 myDatas?
  • 你可以建立一个索引客户端,但你真的必须在客户端做所有事情吗?为什么不简单地询问服务器哪一个可能是搜索原始数据的好工具?
  • 程序很大,我只是想尽可能改进它(想知道我是否可以创建后台作品)。是的,myDatas = 200 MB 我的应用程序基于 javascript(无服务器)
  • @Valeri:这个问题需要很长时间吗?或者是它在处理时阻塞 UI 的问题。换句话说,如果 UI 是响应式的,你关心它需要多长时间吗?
  • @user1689607 当我研究一个单词时(50 000 次出现),Chrome 需要 5 分钟,IE9 被阻止

标签: javascript regex html web-worker


【解决方案1】:

如果问题是 UI 在处理过程中被冻结,您可以将处理分解为异步处理的块。这将使 UI 响应用户。

var i = 0,
    chunkSize = 10, // This is how many to process at a time
    duration = 30;  // This is the duration used for the setTimeout

(function process() {
    // process this chunk
    var chunk = myDatas.slice(i, i += chunkSize);

    // do the next chunk
    if (i < myDatas.length)
        setTimeout(process, duration);
})();

您可以根据需要调整chunkSize 和duration。

如果问题是为了提高实际代码性能,你需要展示一些代码。

【讨论】:

    【解决方案2】:

    answer posted by user1689607 很有趣,但使用 setTimout 会 不要异步执行代码。它将在相同的平台上运行 线程作为主要应用程序。使用计时器还有什么有趣的地方 就是申请的其他部分会有一些处理 定时器运行时可用的时间。

    让您的应用程序在慢速/快速设备上正常运行的最佳方法是 选择处理时间和“发布”时间。 由于处理数据可能需要一些时间,因此您还需要 能够取消它。 并使用回调来处理进程的结束。

    代码可能看起来像这样:

    var DataProcessor = {
         dataProcessDuration : 30,
         releaseDuration : 5,
         dataProcessedCallback : function() { 
                    //... somehow notify the application the result is available
                                           },
         _currentIndex : 0,
         _processing : false,
         _shouldCancel : false,
         processData : function() 
                                  {   this.cancelProcessing();
                                      this._currentIndex =0;
                                      this._processing = true;
                                      // launch data process now, or give it time to cancel
                                      if (!this._shouldCancel)  this._processData();
                                      else setTimeout(this._processData.bind(this), 
                                                this.releaseDuration+this.dataProcessDuration+1);
                                   },
         cancelProcessing : function() 
                                   { 
                                         if (!this._processing) return;
                                         this._shouldCancel=true;
                                   },
         _processData : function() {
              if (this._shouldCancel) { this._shouldCancel = false ;  
                                        this._processing = false; 
                                        return; }
               var startTime = Date.now();
               // while there's data and processing time  left.
               while  ( ( this._currentIndex < length of data)
                           && (Date.now()-startTime < this.dataProcessDuration) )
                    {    var thisBatchCount=10;
                         while (thisBatchCount--)
                               {  
                                  // ... process the this._currentIndex part of the data
                                 this._currentIndex++;
                                 if ( this._currentIndex == length of data) break;
                                }
                    }
               if (this._currentIndex == (length of data) ) // the end ?
                    {     this._processing = false; 
                          this.dataProcessedCallback()
                     }
               else // keep on processing after a release time
                    setTimeout(this._processData.bind(this), this.releaseDuration);
                               }
    }
    

    Rq :在主流程循环中,您可能希望通过更改 thisBatchCount 来微调粒度。 如果太小,您会通过过度检查 Date.now() 来浪费时间,而太大的内部处理将花费太多时间,并且您将无法获得您选择的处理/发布比率。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多