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

@streamr/quick-dijkstra

v1.2.4

Published

Dijkstra Shortest Path Algorithm implemented as a nodejs native module

Downloads

14

Readme

quick-dijkstra

quick-dijkstra implements the Dijkstra's algorithm as a native C++ library for NodeJS, and as a WebAssembly library for the browsers. Speed tests indicate that the native NodeJS library is approximately 10x faster than the js-graph-algorithms Dijkstra implementation written in pure Javascript. The native NodeJS library is distributed as NPM package @streamr/quick-dijkstra, and the WebAssembly version as @streamr/quick-dijkstra-wasm. Both versions are build from the same codebase (https://github.com/streamr-dev/quick-dijkstra)

NodeJS native library

Installation

The native library gets compiled automatically with npm install

npm install @streamr/quick-dijkstra

Usage from Javascript

const QuickDijkstra = require('@streamr/quick-dijkstra');
let result = QuickDijkstra.calculateShortestPaths([ [0,1,1], [1,2,1], [2,3,1], [3,4,1] ] );
console.log(JSON.stringify(result));

Usage from TypeScript

import * as QuickDijkstra from '@streamr/quick-dijkstra';

let result = QuickDijkstra.calculateShortestPaths([ [2,3,1], [0,2,3], [2,1,4] ]);
console.log(JSON.stringify(result));

Complete usage example

See the examples/native for a complete example of using the native NodeJS library.

Running the unit test

npm test

Running the speed test

Runs a speed test against a pure JS Dijkstra implementation of js-graph-algorithms.

npm run speedtest

Api documentation

Api documentation

WebAssembly library

Installation

npm install @streamr/quick-dijkstra-wasm

Compiling

Pre-compiled WebAssembly files are provided in the NPM package, so compiling the webassembly library is not strictly necessary.

If you wish to compile the webassembly files, first download and install the Emscripten toolkit

Then you can compile the webassembly library by issuing the commands

git clone https://github.com/streamr-dev/quick-dijkstra
cd quick-dijkstra
npm install
npm run npm run wasm-compile

Running the speed test

npm run wasm-speedtest

Usage on web with a script tag

<script src="dijkstraengine.js"></script>
<script src="quickdijkstra-wasm.js"></script>

...

<script>

QuickDijkstraWasm.calculateShortestPaths([ [2,3,1], [0,2,3], [2,1,4] ], ret => 
	{
	console.log(ret);
	}	

</script>

See the complete example at examples/wasm/javascript-web

Usage in React project generated using create-react-app

Installation

npm install @streamr/quick-dijkstra-wasm

A project created with create-react-app uses webpack under the hood, and the webpack config needs to be made editable in order to be able to use quick-dijkstra-wasm. This can be achieved with the react-app-rewired package.

Install react-app-rewired and required webpack plugins:

npm i -D react-app-rewired
npm i -D [email protected]
npm i -D write-file-webpack-plugin
npm i -D [email protected]

The react-app-revired uses the file config-overrides.js in the root directory of the project for modifying the webpack config. Create config-overrides.js in the root directory of the project with the following content:

const CopyWebpackPlugin = require('copy-webpack-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');

module.exports = function override(config, env) {
    if (!config.plugins) {
        config.plugins = [];
	}
	config.module.rules[0] = { parser: { requireEnsure: true } };
	console.log(config.module.rules);

    config.plugins.push(new CopyWebpackPlugin({patterns: [{from: 'node_modules/\@streamr/quick-dijkstra-wasm/dijkstraengine.wasm', to: 'static/js'}]}));
	config.plugins.push(new WriteFilePlugin());

	config.module.rules.push(	{
		test: /dijkstraengine\.js$/,
		loader: "exports-loader",
		options: {
			exports: "Module",
		}
	});

	config.module.rules.push( {
		test: /dijkstraengine\.wasm$/,
		loader: "file-loader",
		options: {
		  publicPath: "build/static/js"
		}
	});

	config.output.futureEmitAssets=false;

    return config;
}

Edit package.json to use react-app-rewired instead of react-scripts or react-scripts-ts:

// package.json
"scripts": {
 -   "start":"react-scripts-ts start",
 +   "start": "react-app-rewired start --scripts-version react-scripts-ts",
 -   "build" "react-scripts-ts build",
 +   "build": "react-app-rewired build --scripts-version react-scripts-ts",
 -   "test": "react-scripts-ts test --env=jsdom",
 +   "test": "react-app-rewired test --env=jsdom --scripts-version react-scripts-ts",
     "eject": "react-scripts-ts eject"
  }

See the complete example at examples/wasm/typescript-react

Complete Usage examples

The folder examples/wasm contains complete examples of using the WebAssembly library in various environments:

  • javascript-node is an example of using the WebAssembly library on NodeJS with JavaScript
  • typescript-node is an example of using the WebAssembly library on NodeJS with TypeScript
  • javascript-web is an example of using the WebAssembly library imported using the script tag on web
  • typescript-webpack is an example of using the WebAssembly library in a TypeScript project with webpack
  • typescript-react is an example of using the WebAssembly library in a React TypeScript project generated using create-react-app

The most complete example is the typescript-webpackit, as it includes an example of converting a network graph into the format required by the library. Pay attention to the webpack.config.js file; custom rules are neede in order to use the WebAssembly library in a WebPack project. Also note that the .wasm file needs to be served by the web server from the same folder together with the bundled JavaScript file.

Api documentation

Api documentation

Acknowledgments

The C++ algorithm was inspired by the GeeksforGeeks tutorial by Shubham Agrawal https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl and the Dijkstra's implementation posted by Michal Forišek at https://www.quora.com/What-is-the-most-simple-efficient-C++-code-for-Dijkstras-shortest-path-algorithm