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

@tykowale/ts-hash-map

v1.2.0

Published

TS Hash Map

Downloads

58,917

Readme

TS Hash Map

Introduction

The native Javascript map is an excellent map to use but runs into the issue of using referential equality, which makes difficult if you want to consider using the key {id: 1} without maintaining the original reference. This uses a well established pattern to efficiently create a Map using deep equality.

Table of Contents

Installation

To use the HashMap class in your projects, you can install it via npm:

npm install @tykowale/ts-hash-map

Usage

Creating a HashMap

You can create a new instance of the HashMap class as follows:

const map = new HashMap<string, number>();

Getting and Setting Values

You can set key-value pairs using the set method and retrieve values by key using the get method:

// Set key-value pairs
map.set('apple', 5);
map.set('banana', 10);

// Get values by key
const appleCount = map.get('apple'); // Returns 5
const bananaCount = map.get('banana'); // Returns 10

Deleting Values

You can delete values by key using the delete method:

// Delete a value by key
map.delete('apple');

Iterating Over Entries

You can iterate over entries in the HashMap using a for...of loop or the forEach method:

// Iterate over entries using a for...of loop
for (const [key, value] of map) {
  console.log(`${key}: ${value}`);
}

// Iterate over entries using the forEach method
map.forEach((value, key) => {
  console.log(`${key}: ${value}`);
});

Size of Map

The size property returns the number of elements in the map

const map = new HashMap<string, number>();

map.size; // returns 0

map.set('apple', 5);

map.size; // returns 1

getOrDefault

Sometimes you want a default value instead of having to check for an undefined value

const map = new HashMap<string, number>();

map.getOrDefault('hello', 'world'); // returns 'world'

getOrThrow

Sometimes you know an element should exist and do not want to deal with an undefined check

const map = new HashMap<string, number>();

map.getOrThrow('hello'); // throws Error

Benchmarks

Comparison between native and ts-hash-map, 100,000 iterations of doing the same operation 100 times on a single map. These benchmarks are for Map<string, string> so an all primitive comparison

| Title | Total Time (ms) | Time per Operation (ms) | Operations per Second | |-------------------|-----------------|-------------------------|-----------------------| | Hash Map Set | 2402 | 0.0002402000 | 4,163,197 | | Hash Map Get | 974 | 0.0000974000 | 10,266,940 | | Hash Map Update | 2207 | 0.0002207000 | 4,531,037 | | Hash Map Delete | 665 | 0.0000665000 | 15,037,593 | | Native Map Set | 1690 | 0.0001690000 | 5,917,159 | | Native Map Get | 18 | 0.0000018000 | 555,555,555 | |Native Map Update| 1184 | 0.0001184000 | 8,445,945 | |Native Map Delete| 96 | 0.0000096000 | 104,166,666 |

These benchmarks are for Map<CustomObject, string>. It assumes to store the object in the map you want deep equality so we use fast-json-stable-stringify then store it in the native m ap as Map<string, string>. This is done with only 10,000 iterations so total time is not apples to apples with the above table.

| Title | Total Time (ms) | Time per Operation (ms) | Operations per Second | |---------------------|------------------|-------------------------|-----------------------| | Hash Map Set | 1274 | 0.0012740000 | 784,929 | | Hash Map Get | 988 | 0.0009880000 | 1,012,145 | | Hash Map Update | 1223 | 0.0012230000 | 817,661 | | Hash Map Delete | 898 | 0.0008980000 | 1,113,585 | | Native Map Set | 6527 | 0.0065270000 | 153,209 | | Native Map Get | 2706 | 0.0027060000 | 369,549 | | Native Map Update | 7904 | 0.0079040000 | 126,518 | | Native Map Delete | 3950 | 0.0039500000 | 253,164 |