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

@graph-ts/graph-lib

v1.6.0

Published

A functional TypeScript graphing library

Downloads

2

Readme

graph-lib

An immutable TypeScript graphing library.

Installation

To add graph-lib to your project using npm:

$ npm install @graph-ts/graph-lib

Getting Started

In graph-lib, graphs and the nodes and edges that comprise graphs are immutable. Rather than mutating the state to, for example, add nodes and edges to a graph, a new graph object is created each time the graph is modified.

import { newGraph, addNodes, addEdge, getNodes, getEdges, Graph, Node, Edge } from '@graph-ts/graph-lib';

const a: Node = { id: 'a' };
const b: Node = { id: 'b' };
const ab: Edge = { id: 'ab', source: 'a', target: 'b' };

const g0: Graph = newGraph();
const g1: Graph = addNodes(g0, [a, b]);
const g2: Graph = addEdge(g1, ab);

g0 === g1; // false
g1 === g2; // false
g0 === g2; // false

getNodes(g0); // []
getEdges(g0); // []

getNodes(g1); // [a, b]
getEdges(g1); // []

getNodes(g2); // [a, b];
getEdges(g2); // [ab]

a.id = 'c'; // throws error, these objects are now immutable
ab.source = 'b'; // throws error, these objects are now immutable

Why immutability?

Predictability! Redux! Time-travel! So many good reasons! I'll get to this part later...

What about performance?

Under the hood, graph-ts uses Immer to enforce immutability. It uses structural sharing, meaning that a completely new graph is not actually generated each time a graph is modified; rather, the parts that are unchanged between the two are structurally shared (the introductory blog post for Immer, found here is an excellent resource if you're curious how this works in practice). The implication here is that you're unlikely to run into performance issues due to immutability. Quoting that Immer introduction:

The usual mantra holds here: It is always better to optimize for Developer Experience then for Runtime Performance, unless proven by measurements that you need to do otherwise.