ncla / LoungeDestroyer

Chrome extension for CS:GO Lounge and DOTA2 Lounge
Other
65 stars 67 forks source link

Performance optimization on market price insert #22

Closed ncla closed 10 years ago

ncla commented 10 years ago

Not too happy how fast this currently is executing, the performance issue is most noticeable on larger backpacks. Suggestions?

$(".item").each(function() {
    if(!$(this).hasClass('marketPriced')) {
        if ($("img.smallimg", this).attr("alt") == self.itemName) {
            $(".rarity", this).html(lowestPrice);
            $(this).addClass('marketPriced');
        }
    }
});

Not good

ncla commented 10 years ago

I am not too bright sometimes. Same $.each for .items already exists when going through backpack items to fetch market price, just reuse that..

ncla commented 10 years ago

So for only fetching prices for backpacks, I did this. If the Item object already has made some friends, they will have the lowest price appended fast, if the Item doesn't have any friends referenced, it will go looking for some.

Item.prototype.insertMarketValue = function(lowestPrice) {
    var self = this;
    if(this.myFriends) {
        for (var index in self.myFriends) {
            var $myLittleItem = $(self.myFriends[index]["item"]);
            $myLittleItem.addClass('marketPriced');
            $myLittleItem.find(".rarity").html(lowestPrice);
        }
    }
    else {
        $(".item").each(function() {
            var $theItem = $(this);
            if(!$theItem.hasClass('marketPriced')) {
                if ($theItem.find("img.smallimg").attr("alt") == self.itemName) {
                    $theItem.find(".rarity").html(lowestPrice);
                    $theItem.addClass('marketPriced');
                }
            }
        });
    }
};

Seems to be working great.

birjj commented 10 years ago

You also could save a bit by using vanilla JS instead of jQuery, although I doubt it would have any visible impact (unless you're running it on 10k+ elements). See this for speed comparison - roughly 600% speed increase on my laptop.

ncla commented 10 years ago

Yeah, anything Vanilla JS > jQuery in performance, which is good for background tasks.