Last active
January 18, 2021 23:20
-
-
Save dortzur/544188808423fc0d4ffc6f6ab019ee5b to your computer and use it in GitHub Desktop.
Reddit "Hot Story" and "Best Comments" algorithms in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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));