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

@alwatr/djb2-hash

v1.1.12

Published

A fast, non-cryptographic hash function based on DJB2.

Readme

@alwatr/djb2-hash

A lightweight, high-performance, and non-cryptographic hash function based on the DJB2 algorithm.

NPM Version NPM Downloads Build Status License

This package provides a single, optimized function, djb2Hash, that implements the DJB2 hash algorithm created by Daniel J. Bernstein. It is designed for speed and is excellent for use cases where a fast, simple, and reliable hash is needed, such as in-memory caches, checksums, or generating unique IDs from strings.

Installation

# yarn
yarn add @alwatr/djb2-hash

# npm
npm install @alwatr/djb2-hash

Usage

The package exports a single function, djb2Hash.

import { djb2Hash } from '@alwatr/djb2-hash';

// Generate a numeric hash from a string
const myHash = djb2Hash('hello world');

console.log(myHash); // Outputs: 2616892229

API Reference

djb2Hash(str: string): number

Generates a 32-bit unsigned integer hash from a given string.

  • str: string: The input string to be hashed.
  • Returns: number - A 32-bit unsigned integer representing the hash of the input string.

The function is deterministic, meaning the same input string will always produce the same output hash.

Performance Optimizations

This implementation of djb2Hash includes several performance enhancements over a naive implementation, making it exceptionally fast:

  1. Right-to-Left Iteration: The function iterates over the string from right to left (for (let i = str.length - 1; ...)). This avoids repeatedly accessing the length property of the string in each loop iteration, which can provide a small but meaningful performance boost, especially for longer strings.
  2. Bitwise Operations: It uses bitwise shifting (<< 5) instead of multiplication (* 33). The expression (hash << 5) + hash is a faster way to compute hash * 33. Modern JavaScript engines are highly optimized for these low-level bitwise operations.
  3. Unsigned Integer Output: The final >>> 0 operation is a zero-fill right shift, which ensures that the output is always a 32-bit unsigned integer. This provides a consistent and predictable return type.

Security Note

djb2Hash is a non-cryptographic hash function.

It is designed for speed and simplicity, not for security. Do not use it for security-sensitive applications such as password hashing or digital signatures. The algorithm is not designed to be collision-resistant against malicious attacks.

For security-critical use cases, always use well-established cryptographic hash functions like SHA-256 or Argon2.