Javascript 提高程序性能
我有一个javascript程序,它使用我的JSON数据(200Mega)执行操作 此程序通过regxp搜索我的数据中的事件Javascript 提高程序性能,javascript,regex,html,web-worker,Javascript,Regex,Html,Web Worker,我有一个javascript程序,它使用我的JSON数据(200Mega)执行操作 此程序通过regxp搜索我的数据中的事件 var myDatas = [ { "id" : "0000000001", "title" :"Data1", "info": { "info1": "data data data", "info2": "infoinfoiinfoinfo", "info3": "info333
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" },
. . . ];
实际执行太慢了
我会使用HTML5的Worker,但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 );
}
}
},
如何改进我的程序?如果问题是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);
})();
var i=0,
chunkSize=10,//这是一次要处理的数量
持续时间=30;//这是用于设置超时的持续时间
(函数过程(){
//处理这个区块
var chunk=myDatas.slice(i,i+=chunkSize);
//做下一块
如果(i
您可以根据需要调整chunkSize
和duration
如果问题是为了提高实际代码性能,则需要显示一些代码。这很有趣,但使用设置超时将
不异步执行代码。它将在同一台计算机上运行
线程作为主应用程序。使用计时器还有什么有趣的地方
应用程序的其他部分将进行一些处理
计时器运行时可用的时间
让应用程序在慢速/快速设备上正常工作的最佳方法是
选择处理时间和“释放”时间。
由于处理数据可能需要一些时间,您还需要
我可以取消它。
并使用回调来处理进程的结束
代码可以是这样的:
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);
}
}
var数据处理器={
数据处理持续时间:30,
发布时间:5,
dataProcessedCallback:函数(){
//…以某种方式通知应用程序结果可用
},
_当前索引:0,
_处理:假,
_应该取消:false,
processData:函数()
{this.cancelProcessing();
这是.\u currentIndex=0;
这是。_processing=true;
//立即启动数据处理,或给它时间取消
如果(!this.\u应该取消)this.\u processData();
else setTimeout(this.\u processData.bind(this),
this.releaseDuration+this.dataProcessDuration+1);
},
取消处理:函数()
{
如果(!this.\u处理)返回;
这是.\u shouldCancel=true;
},
_processData:函数(){
如果(this.\u shouldCancel){this.\u shouldCancel=false;
这是。_processing=false;
返回;}
var startTime=Date.now();
//还有数据和处理时间。
while((此._currentIndex<数据长度)
&&(Date.now()-startTime
Rq:在主流程循环中,您可能希望通过更改thisBatchCount来微调粒度。
如果太小,您会因过度检查Date.now()而浪费时间,而太大,内部处理将花费太多时间,您将无法获得您选择的处理/释放比率。此外,它提取了什么?myDatas
是完整的200MB吗?或者您想在200 MB中查找myDatas
?您可以在客户端构建索引,但您真的必须在客户端完成所有工作吗?为什么不简单地问一下服务器,它可能有很好的工具来搜索原始数据?程序非常大,我只想在可能的情况下改进它(知道我是否可以创建背景作品)。是的,myDatas=200 MB我的应用程序是基于javascript的(没有服务器)@Valeri:问题是它需要很长时间吗?或者是它在处理时阻塞UI的问题。换句话说,如果用户界面是有响应的,你关心它需要多长时间吗?@user1689607当我研究一个单词(50000次出现)时,Chrome需要5分钟,IE9被阻止