提高计划绩效

时间:2012-11-15 14:44:47

标签: javascript regex html5 web-worker

我有一个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 );
            }
        }
},

我如何改进我的计划?

2 个答案:

答案 0 :(得分: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);
})();

您可以根据需要调整chunkSizeduration

如果问题是要提高实际代码性能,则需要显示一些代码。

答案 1 :(得分:1)

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()来浪费时间,并且内部处理太大会花费太多时间而你将没有你选择的过程/释放比率。