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.1.12

Published

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

Downloads

848

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

Feedback & Contributions

We welcome all types of feedback and contributions! If you have any of the following, please feel free to open an issue

Your input is valuable to us and helps improve the project for everyone. Thank you for contributing!

Features

BiMap

OneToManyMap

Tree

UndirectedGraph