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 🙏

© 2025 – Pkg Stats / Ryan Hefner

frecency-sort

v1.0.9

Published

Generic frecency sorting algorithm using localstorage

Downloads

341

Readme

Frecency Sort

NPM

Generic frecency sorting solution using localStorage based on the popular blog post by Slack.

Table of Contents

What is frecency?

"Frecency" is a term which combines "recency" and "frequency". While I can recommend the above post for a more in-depth explanation, essentially this algorithm sorts options in a list based on a mix of how much they are used, and how frequently they are used.

The aim of this approach is to offer the user a selection of items which seems to learn from their behaviour, shortening the user's time to find what they need.

Installation

To get started, install by running

npm i frecency-sort

(replace with package manager of your choice).

Using this library

This library will sort a given list of options by the frecency with which each option has been picked for a given search query.

As an example, if a user searches for "John" and initially sees the following options:

  • John Cena
  • John Doe

They might be frustrated if they always want "John Doe" and have to click/scroll to the 2nd option.

Frecency allows us to "remember" that the search for "John" resulted in the user selecting "John Doe", letting us prioritize "John Doe" the next time "John" is searched for.

A note on text searching

Frecency does not perform filtering/searching of options, only ordering.

As such, for most common use cases, you will probably want to filter your list of options before passing them to sortByFrecency, usually based on some (potentially fuzzy) text matching.

In the example above, the part where searching for "John" results in the two options of "John Cena" and "John Doe" should be part of your own business logic.

Implementation example

A generic implementation, including basic searching, could look something like this:

const myLocalStorageKey = "my-frecency-data"
const users = [
    {matchingId: "some-guid", name: "John Cena"},
    {matchingId: "some-other-guid", name: "John Doe"}
]  
const searchQuery = "John"
const matchingUsers = users.filter(u => u.name.includes(searchQuery))
const sortedUsers = sortByFrecency(searchQuery, matchingUsers, myLocalStorageKey)

Note that the above code will always return the same (original) order until you start storing frecency data:

Storing frecency data

Frecency sorting only makes sense if we have some data about the user's behaviour. Therefore, this library exposes updateFrecencyData(), which you are expected to call whenever a user selects an option.

To continue the above code example, when redering sortedUsers, you would most likely do something like this:

sortedUsers.map(user => 
    <div 
        key={user.id}
        onClick={() => {
            updateFrecencyData(searchQuery, user.id, myLocalStorageKey) // Update frecency data for future searches
            selectUser(user) // Whatever business logic you need when an option is clicked
        }}
    >
        {user.name}
    </div>
)

Shouldn't frecency calculations happen in the API, and be stored in a DB?

This library simply uses localStorage in the user's browser to store frecency information. This is easy to use and works especially well for cases where the data set is always known in full on the frontend. Examples include:

  • Navigation options (pages)
  • Any smaller set of data which isn't paginated (user favourites, team members etc.)

If you want to get the most frecent sub-set of a larger dataset, you're better off implementing frecency in your API directly, and storing frecency data in a DB.