PDA

View Full Version : Making My Paste Code More Efficient



mike000
20 Jul 2007, 9:43 AM
Hi, I have some code that I'm using to implement pasting from Excel into a custom EditorGrid I've made. The problem is that the code is very slow for large data and I can't think of any good ways to improve its efficiency. I've done all the dumb tricks that I know of such as code motion (taking arithmetic out of the inner most loop) and it's been to no avail.

When we did something similar in an assignment in C, it was a good idea to paste in blocks very close to the page size, but I don't think that's applicable here (especially beacuse I don't think my data is nealry that large... and since it's Javascript and not C).

Anyways, the two lines that really slow the algorithm down are:
r.set(field, e.value);

and

this.fireEvent("afteredit", e);

I am able to comment out the afterEdit line and the code still works, although this is probably not the greatest idea. But I need to have that r.set line in there. Is there a way to maybe set multiple properties in the record at once so that it's less time consuming? I'd also be willing to hear any completely different algorithms that you might suggest.

Here is the code. Note that those bY and bX variables deal with blocks I'm using for replicating Excel's ability to fill in a selected block that is a multiple of what you're pasting with data.


var rows = strText.split('\n'); //Split by lines
var yBound = (bY+1) * pYSize;
for (y=basebY; y<yBound; y++) {

var r = this.dataSource.getAt(row + y);
var cols = rows[y - basebY].split('\t'); //Split by tabs

var xBound = (bX + 1) * pXSize;
var x;
for (x=(basebX); x<xBound; x++) {

var getX = x - basebX;

var field = this.colModel.getDataIndex(col+x);

var orgValue;
var orgRec;
orgRec = this.dataSource.getAt(row);
orgValue = orgRec.data[field];

var e = {
grid: this,
record: r,
field: field,
originalValue: orgValue,
value: cols[getX],
row: row+y,
column: col+x,
cancel:false
};
if(this.fireEvent("validateedit", e) !== false && !e.cancel){
r.set(field, e.value); //slow!
delete e.cancel;
this.fireEvent("afteredit", e); //slow!
}

} //for x
} //for y

mike000
24 Jul 2007, 12:24 PM
I found that I was calling grid.view.refresh() each time in afterEdit, which was the main reason for the slow down.

Additionally, I found it faster to manually update the records and then do a refresh rather than call r.set() each call. Here's how I improved that line:



if(this.fireEvent("validateedit", e) !== false && !e.cancel){
r.data[field] = e.value;
delete e.cancel;
this.fireEvent("afteredit", e);
}


And then after everything was done:



this.view.refresh();