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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cur10sdev/base62-encoder-decoder

v1.0.1

Published

High-performance base62 encoder/decoder for JavaScript and Node.js. Supports Number and BigInt inputs, optimized for both time and space complexity.

Downloads

21

Readme

base62-encoder-decoder

npm version License: MIT

High-performance base62 encoder/decoder for JavaScript and Node.js. Optimized for both time and space complexity with support for Number and BigInt inputs.

Features

  • 🚀 High Performance - Optimized algorithms with O(n) decode and O(log n) encode
  • 📦 Lightweight - Minimal dependencies, ~2KB minified
  • 🔢 BigInt Support - Handle numbers larger than Number.MAX_SAFE_INTEGER
  • 🛡️ Type Safe - Full TypeScript support with detailed type definitions
  • Flexible - Supports any base from 2 to 62
  • 📚 Well Tested - Comprehensive test suite with edge cases

Installation

npm install @cur10sdev/base62-encoder-decoder

Usage

Basic Encoding/Decoding

const { encode, decode } = require("@cur10sdev/base62-encoder-decoder");

// Encode a number to base62
const encoded = encode(12345);
console.log(encoded); // "3d7"

// Decode base62 back to number
const decoded = decode("3d7");
console.log(decoded); // 12345

BigInt Support

const { encode, decode } = require("@cur10sdev/base62-encoder-decoder");

// Handle very large numbers
const largeNum = BigInt("123456789012345678901234567890");
const encoded = encode(largeNum);
const decoded = decode(encoded);

console.log(decoded === largeNum); // true

Custom Base

const { encode, decode } = require("@cur10sdev/base62-encoder-decoder");

// Encode in base 16 (hexadecimal)
const hex = encode(255, 16);
console.log(hex); // "ff"

// Encode in base 2 (binary)
const binary = encode(8, 2);
console.log(binary); // "1000"

// Encode in base 36 (alphanumeric)
const base36 = encode(1000, 36);
console.log(base36); // "rs"

TypeScript

import { encode, decode, CHARACTERS } from "@cur10sdev/base62-encoder-decoder";

const encoded: string = encode(12345);
const decoded: number | bigint = decode(encoded);

console.log(CHARACTERS); // "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

API Reference

encode(value: number | bigint, base?: number): string

Encodes a number to a base-N string representation.

Parameters:

  • value (number | bigint): The number to encode. Must be non-negative.
  • base (number, optional): The base to use (default: 62). Must be between 2 and 62.

Returns: The encoded string representation.

Throws:

  • TypeError: If value is not a Number or BigInt
  • RangeError: If value is negative or base is out of range

Time Complexity: O(log n) where n is the input value Space Complexity: O(log n) for the output string

decode(str: string, base?: number): number | bigint

Decodes a base-N string back to a number.

Parameters:

  • str (string): The encoded string to decode
  • base (number, optional): The base of the input string (default: 62). Must be between 2 and 62.

Returns: The decoded number (Number if safe, BigInt otherwise)

Throws:

  • TypeError: If str is not a string
  • RangeError: If str contains invalid characters for the given base

Time Complexity: O(n) where n is the string length Space Complexity: O(1)

CHARACTERS: string

The character set used for base62 encoding.

"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Performance

The implementation is highly optimized:

| Operation | Time | Space | | ----------- | -------- | -------- | | encode(n) | O(log n) | O(log n) | | decode(str) | O(n) | O(1) |

Benchmarks on typical inputs:

  • Encoding 12-digit numbers: ~0.05ms
  • Decoding 11-character strings: ~0.03ms
  • Handling Number.MAX_SAFE_INTEGER: ~0.1ms
  • Handling large BigInt values: ~0.15ms

Use Cases

  • URL Shortening: Generate short, human-readable identifiers from numeric IDs
  • ID Generation: Create compact representations of large integers
  • Database Keys: Convert numeric primary keys to base62 strings
  • Data Compression: Represent binary data in a human-readable format
  • Game Development: Generate short codes for multiplayer sessions or achievements

Example: URL Shortening

const { encode, decode } = require("@cur10sdev/base62-encoder-decoder");

// Simulate a URL shortening service
class URLShortener {
  constructor() {
    this.counter = 1;
    this.urls = new Map();
  }

  shorten(longUrl) {
    const id = this.counter++;
    const shortCode = encode(id);
    this.urls.set(shortCode, longUrl);
    return `https://short.url/${shortCode}`;
  }

  expand(shortCode) {
    const id = decode(shortCode);
    return this.urls.get(shortCode);
  }
}

const shortener = new URLShortener();
const short = shortener.shorten("https://example.com/very/long/url");
console.log(short); // "https://short.url/1"

License

MIT © Your Name

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please open an issue on GitHub.