Skip to content

Instantly share code, notes, and snippets.

@dortzur
Last active January 18, 2021 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dortzur/544188808423fc0d4ffc6f6ab019ee5b to your computer and use it in GitHub Desktop.
Save dortzur/544188808423fc0d4ffc6f6ab019ee5b to your computer and use it in GitHub Desktop.
Reddit "Hot Story" and "Best Comments" algorithms in JavaScript
//Based off of https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9
function hotStory(ups, downs,date){
const score = ups - downs;
const order = Math.log(Math.max(Math.abs(score), 1), 10);
const sign = score > 0 ? 1 : ((score < 0) ? -1 : 0);
const seconds = (date.getTime()/1000) - 1134028003;
return Math.round(sign * order + seconds / 45000, 7);
}
function bestComment(ups, downs){
const n = ups + downs;
if (n == 0){
return 0;
}
const z = 1.281551565545;
const p = ups / n;
const left = p + 1/(2*n)*z*z;
const right = z* Math.sqrt(p * (1-p) / n + z * z / ( 4 * n * n ));
const under = 1+1/n*z*z;
return (left - right) / under;
}
@jackslocum
Copy link

Thanks for sharing! One minor tweak:
const order = Math.log(Math.max(Math.abs(score), 1), 10);
should be:
const order = Math.log10(Math.max(Math.abs(score), 1));

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