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 🙏

© 2024 – Pkg Stats / Ryan Hefner

relation-map

v1.0.3

Published

A flexible relational mapping library supporting 1:1 and 1:N

Downloads

52

Readme

Table of contents

Relation map

A flexible relational mapping library supporting 1:1 and 1:N

Install

npm i relation-map
yarn add relation-map

Link

npm

github

Problem and Solve

Performance due to time complexity

run from codeSandbox

  • When we search a value from typical (unidirectional) map, its time complexity is O(N).
  • This is because an exhaustive search is required to access and manipulate values ​​rather than keys.
  • But BiMap is O(1). Moreover, updating, deleting and adding are also of the same complexity.
import { BiMap } from "relation-map";

const biMap = new BiMap();
const map = new Map();
const N = 12345;
for (let i = 0; i < N; i++) {
  map.set(String(i), i);
  biMap.set(String(i), i);
}
const mapTime = { total: 0, average: 0 };
const biMapTime = { total: 0, average: 0 };

const start1 = performance.now();
for (let i = 0; i < N; i++) {
  // To find value, A complete search is required.
  for (const v of map.values()) {
    // If find, stop
    if (v === i) break;
  }
}
const end1 = performance.now();
mapTime.total = end1 - start1;
mapTime.average = (end1 - start1) / N;

const start2 = performance.now();
for (let i = 0; i < N; i++) {
  // Find with O(1) complexity.
  biMap.getByRight(i);
}
const end2 = performance.now();

// Log in ms
biMapTime.total = end2 - start2;
biMapTime.average = (end2 - start2) / N;
console.log({ mapTime });
console.log({ biMapTime });
  • The difference in results is stark!
{
  mapTime: { total: 340.80774399999996, average: 0.027606945646010527 }
}

{
  biMapTime: { total: 1.2093150000000037, average: 0.000097959902794654 }
}

Boilerplate to maintain relationship

  • To solve time complexity, you can consider creating a reference data structure.
  • However, each time you update, delete, or add data, the amount of code you will need to write will double.
const map = new Map();

const map1 = new Map();
const map2 = new Map(); // for reference to solve time complexity of map1.
// Add data pair
for (let i = 0; i < 10000; i++) {
  map.set(String(i), i);

  map1.set(String(i), i);
  map2.set(i, String(i)); // Also, need to set it on map2.
}
// Update
map.set("10", 7);
map.delete("7"); // To maintain 1:1

map1.set("10", 7);
map2.set(7, "10");
// Also, need to maintain 1:1 on both.
map1.delete("7");
map2.delete(10);
  • This is a simple example.
  • However, in actual work, there is the following problem:
    • As the amount of code increases, the probability of bugs increases.
    • As the amount of code increases, stamina will be depleted.
    • As the amount of code increases, project structure will be dirty.
  • We can work more simply with a relation map!
const biMap = new BiMap();
// ...
biMap.set("10", 7); // End

Features

BiMap

  • A one-to-one relationship between "left" and "right" is maintained when updating, adding and deleting.
  • Quick modification and access to "left" and "right" are possible.
  • Duplication is not allowed for the same "left" and "right".

Term Explanation

There is basically no priority between "left" and "right". At the developer's discretion, they can be prioritized conceptually.

  • left: This value is used as the left key in the BiMap. Each left can only be mapped to a single right. You can use the left to look up or delete the corresponding right.

  • right: This value is used as the right key in the BiMap. Each right can only be mapped to a single left. You can use the right to look up or delete the corresponding left.

Usage

create instance

  • Set type of left and right with generic<L, R>.
  • Default type is any.
const myMap = new BiMap<string, number>();

set

  • Add or update the pair.
myMap.set("a", 10);

delete

  • Delete the pair.
myMap.set("a", 10);
myMap.set("b", 5);
myMap.deleteByLeft("a"); // delete "a" & 10
myMap.deleteByRight(5); // delete "b" & 5

get

  • Gets the value in the opposite direction.
myMap.set("a", 10);
myMap.getByLeft("a"); // 10
myMap.getByRight(10); // a

has

  • Check if a value exists.
const myMap = new BiMap<string, number>();
myMap.set("a", 10);
myMap.hasByLeft("a"); // true
myMap.hasByRight(20); // false

clear

  • Clear all data.
myMap.clear();

size

  • One pair size.
const myMap = new BiMap<string, number>();
myMap.set("a", 10);
myMap.set("b", 5);
myMap.size(); // 2

get member variables

  • Get "readonly" member variables.
  • Modification may result in unexpected behavior.
myMap.getLeftToRight();
myMap.getRightToLeft();

OneToManyMap

  • A 1:N relationship between "one" and "many" is maintained when updating, adding and deleting.
  • Quick modification and access to "one" and "many" are possible.
  • Duplication is not allowed for the same "one" and "many".

Term Explanation

  • one: Represents a unique key in the OneToManyMap structure, which can be associated with multiple many. Each one can have several corresponding many values, but each many value can only belong to a single one. For example, in a teacher-student relationship, a teacher (one) can have multiple students (many), but each student is linked to only one teacher in this context.

  • many: Represents values that are associated with a single one. Each many value is connected to only one one, ensuring that it does not belong to multiple one keys simultaneously. This is crucial for maintaining the 1 relationship where one key is mapped to multiple values without overlapping with other keys. Using the teacher-student analogy, each student (many) is linked to one specific teacher (one).

Usage

create instance

const myMap = new OneToManyMap<string, number>();

set

  • Initialize or add the pair.
  • If "one" does not exist, it will be added. And "many" is added.
  • If "one" exists, "many" is added.
myMap.set("a", 10);

delete

  • Delete "one" and all "many" related to "one".
myMap.set("a", 10);
myMap.set("a", 20);
myMap.set("a", 30);
myMap.deleteByOne("a"); // delete "a" & 10 & 20 & 30
  • Delete only "many".
myMap.set("a", 10);
myMap.set("a", 20);
myMap.set("a", 30);
myMap.deleteByMany(20); // delete 20, no relation between "a" & 20.

get

  • Get many related to "one" as Set<M>
myMap.set("a", 10);
myMap.set("a", 20);
myMap.set("a", 30);
myMap.getByOne("a"); // {10, 20, 30}
  • Get one related to "many"
myMap.set("a", 10);
myMap.set("a", 20);
myMap.set("a", 30);
myMap.getByMany(20); // "a"

has

  • Check if a value exists.
const myMap = new OneToManyMap<string, number>();
myMap.set("a", 10);
myMap.hasByOne("a"); // true
myMap.hasByMany(20); // false

clear

  • Clear all data.
myMap.clear();

size

  • Get size.
const myMap = new OneToManyMap<string, number>();
myMap.set("a", 10);
myMap.set("a", 5);
myMap.sizeOne(); // 1
myMap.sizeMany(); // 2

get member variables

  • Get "readonly" member variables.
  • Modification may result in unexpected behavior.
myMap.getOneToMany();
myMap.getManyToOne();

count

  • Count how many relationships there are for each “one”.
const myMap = new OneToManyMap<string, number>();
myMap.set("a", 10);
myMap.set("a", 5);
myMap.set("b", 20);
myMap.count(); // {"a" => 2, "b" => 1}