Skip to content

Instantly share code, notes, and snippets.

@simevidas
Last active July 12, 2017 17:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simevidas/a88463cb508e93bb37d7 to your computer and use it in GitHub Desktop.
Save simevidas/a88463cb508e93bb37d7 to your computer and use it in GitHub Desktop.
A JS snippet for highlighting tweets with lots of RTs or hearts
(function(){
'use strict';
let cap = 100; // 100+ RTs or hearts produces max yellow bg color
$('.tweet', '.stream-items').each((i, tweet) => {
let all_nums = $(tweet)
.find('.ProfileTweet-actionList .ProfileTweet-actionCount:visible')
.map((j, elem) => Number(elem.textContent)).toArray();
let num = Math.min(Math.max.apply(Math, all_nums.concat(0)), cap);
let str = Math.floor(255 * (1 - num/cap)).toString(16);
if (str.length === 1) str = '0' + str;
$(tweet).css({ 'background-color': '#FFFF' + str });
});
}());
@eorroe
Copy link

eorroe commented Feb 13, 2016

Cache the $(tweet) you're calling it twice in line #7 and #17

@eorroe
Copy link

eorroe commented Feb 13, 2016

And you only need one find(): $(tweet).find('.ProfileTweet-actionList .ProfileTweet-actionCount:visible')

@simevidas
Copy link
Author

Caching $(foo) is a micro-optimization. It only takes about 5 microseconds, so you can have dozens of them in a code block without affecting performance.

I’ve switched to one .find(); seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment