npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decay

v1.0.12

Published

Famous sorting algorithms based on vote popularity and time

Downloads

839

Readme

decay

npm status build status dependency status coverage status

This library houses 3 popularity estimating algorithms employed by bigger news sites used to sort for best content:

  1. wilsonScore - Reddit's best comment scoring system
  2. redditHot - Reddit's hot post scoring system for news posts
  3. hackerHot - Hackernews' scoring system

Wilson score equation

Algorithms may cause scores to decay based on distance to post time.

1. Decaying algorithms

Algorithms that are designed to decay based on time needs continual recomputation of scores. An example of doing so would be keeping track of, and periodically computing the score(s) required in a node process on a set of suitable candidates:

var decay = require('decay')
  , hotScore = decay.redditHot();

setInterval(function () {
  candidates = []; // perhaps get recent posts saved in db here
  candidates.forEach(function (c) {
    c.score = hotScore(c.upVotes, c.dnVotes, c.date);
    // save so that next GET /entry/ gets an updated ordering
    save(c);
  });
}, 1000 * 60 * 5); // run every 5 minutes, say

2. Non-decaying algorithms

Algorithms that produce a time agnostic popularity score is typically good for comments. For best results, simply recompute the score at every new vote:

var decay = require('decay')
  , wilsonScore = decay.wilsonScore();

// assume req.entry is the item being voted on
app.post('/entry/upvote', middleWare, function (req, res) {
  // call wilsonScore with ups, downs, post_date to recompute
  req.entry.score = wilsonScore(req.entry.upVotes + 1, req.entry.dnVotes, req.entry.postDate);

  // save new score in database so that new pageviews sort
  save(req.entry);
});

Usage

Decay exports 3 scoring function factories.

Two of these algorithms decay with time, and the other is based purely on statistical popularity.

// 1. zero decay
var wilsonScore = decay.wilsonScore(zScore);
var score = wilsonScore(upVotes, downVotes);

// 2. decays
var redditHotScore = decay.redditHot(halflife);
var score = redditHotScore(upVotes, downVotes, date);

// 3. decays
var hackerHotScore = decay.hackerHot(gravity);
var score = hackerHotScore(upVotes, date);

Parameter Explanation

1. Wilson Score

AKA Reddit's Best comment sorting system. Source

Statistically, it is the lower bound of the Wilson Score interval at the alpha level based on supplied Z score.

The optional zScore parameter can be passed as to the exported wilsonScore factory. The Z score is a statistical value which roughly means how many standard deviations of safety you want, so it maps directly onto the confidence level of the Wilson Score interval.

It will default to z=1.96 if left out, representing a 95% confidence level in the lower bound. Otherwise, values through 1.0 (69%), to 3.3 (99.9%) good alternatives.

2. Reddit Hot Sort

Based on the difference between ups/downs, and decays with time. Causes hive mind effects in large crowds.

An optional halflife parameter can be passed to the exported redditHot factory. The half-life defaults to 45000 [s]. For info on the effects on this parameter read the original blog post about it. See also the canonical reddit source version.

3. HackerNews Hot Sort

Based on simply the amount of upvotes, and decays with time. Prone to advertising abuse.

An optional gravity parameter (defaulting to 1.8) can be passed to the exported hackerHot factory. For info on the effects of this parameter read the original blog post about it.

Installation

$ npm install decay

License

MIT-Licensed. See LICENSE file for details.