🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@tykowale/ts-hash-map

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tykowale/ts-hash-map

TS Hash Map

1.2.0
latest
Source
npm
Version published
Weekly downloads
12K
-21.06%
Maintainers
0
Weekly downloads
 
Created
Source

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
  • Usage
  • Benchmarks

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

TitleTotal Time (ms)Time per Operation (ms)Operations per Second
Hash Map Set24020.00024020004,163,197
Hash Map Get9740.000097400010,266,940
Hash Map Update22070.00022070004,531,037
Hash Map Delete6650.000066500015,037,593
Native Map Set16900.00016900005,917,159
Native Map Get180.0000018000555,555,555
Native Map Update11840.00011840008,445,945
Native Map Delete960.0000096000104,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.

TitleTotal Time (ms)Time per Operation (ms)Operations per Second
Hash Map Set12740.0012740000784,929
Hash Map Get9880.00098800001,012,145
Hash Map Update12230.0012230000817,661
Hash Map Delete8980.00089800001,113,585
Native Map Set65270.0065270000153,209
Native Map Get27060.0027060000369,549
Native Map Update79040.0079040000126,518
Native Map Delete39500.0039500000253,164

FAQs

Package last updated on 28 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts