Slow as molasses, unfortunately
![]() ![]() |
Looks like it has an O(N^2) loop. |
![]() ![]() |
for each (var d in allDivs) {
if (!d.className)
continue;
var classes = d.className.split(" ");
if (!classes[1])
continue;
var votes = voteTable[classes[1].substring(3)];
if (!votes)
continue;
// Found the div, find the first span with class "score likes"
var spans = d.getElementsByTagName("span");
I removed the inner loop like above (the code following spans remains the same except use "votes" instead of "voteTable[name]" and remove the "delete voteTable[name]" line). Speed improved tremendously. I hope this is right. |
![]() ![]() |
You might consider breaking up the synchronous loop into a tail-calling function using setTimeout. THat'll prevent the browser from locking during the modifications. |
![]() ![]() |
chengiz, could you clarify your instructions for the javascript-challenged? I just replaced the code with yours, changed voteTable[name] to votes and removed the one line, but that didn't work. |
![]() ![]() |
By not working do you mean it (a) doesnt speed up or (b) doesnt work period? If (a), I cant help you much -- it depends on the firefox version (I tend to use an older one) and the number of comments displayed and so on. If (b), you might have missed a step, I apologize for the incomplete instructions. I'm doing this from memory, so hope it's right. From the original source:
...
scoreSpan = s;
var votes = voteTable[name]; // Delete this line (you might have missed it) - chengiz
...
count++;
if(count > 1) {
delete voteTable[name]; // Delete this line (you should have done this) - chengiz
break;
}
...
|
![]() ![]() |
Updated the code. Now runs significantly faster on both Linux and Windows - FF3.5. Should run just as fast on FF3.x. Replace the functions in your script defined in the code below. Leave other functions in tact.
//Recursively process the comment tree
function processTree(obj) {
if(obj instanceof Array) {
for each (var tree in obj) {
processTree(tree);
}
}
var data = obj.data;
if(data) { //Data found
if(isComment(obj)) {
var name = data.name;
if(name) { //Store the votes in the vote table
var ups = 0, downs = 0;
if(data.ups) ups = data.ups;
if(data.downs) downs = data.downs;
voteTable[name] = {downs:downs, ups:ups};
}
}
//Process any subtrees
processChildren(data);
processReplies(data);
}
}
//Display the votes from the vote table
function displayVotes() {
//Add the style sheets for up and down ratings
GM_addStyle(".moo_ups { color:rgb(255, 139, 36); font-weight:bold; }");
GM_addStyle(".moo_downs { color:rgb(148,148,255); font-weight:bold; }");
for (var name in voteTable) {
var div = document.getElementsByClassName("id-"+name);
if(div.length == 1){
if(spans[0] && spans[1]){
//Found the div, find the first span with class "score likes"
var spans = div[0].getElementsByClassName("score likes");
var count = 0;
var votes = voteTable[name];
var parent = spans[0].parentNode;
//Append ups and downs
//spans[0] is for collapsed comments, spans[1] is for expanded comments
//this part is f-in slow
var openparen = document.createTextNode(" (");
parent.appendChild(openparen);
var mooups = document.createElement("span");
mooups.className = "moo_ups";
var voteUps = document.createTextNode(votes.ups);
mooups.appendChild(voteUps);
parent.appendChild(mooups);
var pipe = document.createTextNode("|");
parent.appendChild(pipe);
var moodowns = document.createElement("span");
moodowns.className = "moo_downs";
var voteDowns = document.createTextNode(votes.downs);
moodowns.appendChild(voteDowns);
parent.appendChild(moodowns);
var closeparen = document.createTextNode(")");
parent.appendChild(closeparen);
parent = spans[1].parentNode;
parent.appendChild(openparen);
parent.appendChild(mooups);
parent.appendChild(pipe);
parent.appendChild(moodowns);
parent.appendChild(closeparen);
}
}
}
}
function processChildren(data) {
var children = data["children"];
if(children) {
//Process the children
for(var i=0, il=children.length; i < il; i++){
processTree(children[i]);
}
}
}
|
![]() ![]() |
Thanks Allan, but it looks like your code got cut off at the end. I'm pretty sure that's not a complete processChildren function. Any plans for these changes to be integrated with the main script soon? |
![]() ![]() |
Thanks ausiv for noticing. The HTML interpreter picked up on i < il (without spaces) and made it i<il> I've emailed the code originator but he hasn't messaged me back, that's why I included the comment here. I'll gladly post the code on my website and provide a link if you would like, or I can create a new project here. I would really like for the code to be incorporated, but I know how hard it is for the original developers to get around to things. |
![]() ![]() |
This updated script handles processing of 550+ comments in only 847ms average (on testing machine). It works on your comment submission page and doesn't break on deleted comments. Please get it here: http://userscripts.org/scripts/show/56641 My previous script, submitted in the above comment processed 550+ comments in 6800ms, which is horrible but was still better than the original script. |


