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

@beanjs/trek-router

v1.2.1

Published

A fast HTTP router

Downloads

6

Readme

trek-router

A fast HTTP router, inspired by Echo's Router.

Benchmarks

See benchmarks, use GitHub API Routes and Discourse API Routes.

VS

$ sysctl machdep.cpu.brand_string
machdep.cpu.brand_string: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
$ sysctl hw.memsize
hw.memsize: 34359738368
$ yarn run bench
yarn run v0.22.0
$ node benchmarks

GitHub API, 203 routes:
trek-router x 12,652 ops/sec ±0.86% (91 runs sampled)
memoryUsage: { rss: 69746688,
  heapTotal: 46108672,
  heapUsed: 31815272,
  external: 9284 }
path-to-regexp x 973 ops/sec ±0.21% (95 runs sampled)
memoryUsage: { rss: 87674880,
  heapTotal: 63422464,
  heapUsed: 52531280,
  external: 9284 }
route-recognizer x 795 ops/sec ±1.18% (95 runs sampled)
memoryUsage: { rss: 102465536,
  heapTotal: 77578240,
  heapUsed: 44904632,
  external: 9284 }
route-trie x 2,515 ops/sec ±1.64% (94 runs sampled)
memoryUsage: { rss: 100438016,
  heapTotal: 75481088,
  heapUsed: 31334832,
  external: 9284 }
routington x 1,896 ops/sec ±0.84% (95 runs sampled)
memoryUsage: { rss: 100835328,
  heapTotal: 75993088,
  heapUsed: 24018936,
  external: 9284 }
wayfarer x 2,327 ops/sec ±0.80% (97 runs sampled)
memoryUsage: { rss: 101703680,
  heapTotal: 77041664,
  heapUsed: 20170432,
  external: 9284 }
Fastest is trek-router

Discourse API, 359 routes:
trek-router x 8,665 ops/sec ±0.33% (97 runs sampled)
memoryUsage: { rss: 101941248,
  heapTotal: 77041664,
  heapUsed: 37168328,
  external: 9284 }
path-to-regexp x 115 ops/sec ±0.43% (83 runs sampled)
memoryUsage: { rss: 106766336,
  heapTotal: 81772544,
  heapUsed: 33245496,
  external: 9284 }
route-recognizer x 534 ops/sec ±0.12% (95 runs sampled)
memoryUsage: { rss: 107204608,
  heapTotal: 81772544,
  heapUsed: 43184720,
  external: 9284 }
route-trie x 2,183 ops/sec ±0.49% (96 runs sampled)
memoryUsage: { rss: 106786816,
  heapTotal: 81248256,
  heapUsed: 34731560,
  external: 9284 }
routington x 1,487 ops/sec ±0.53% (95 runs sampled)
memoryUsage: { rss: 107184128,
  heapTotal: 81772544,
  heapUsed: 22843088,
  external: 9284 }
wayfarer x 1,966 ops/sec ±0.37% (96 runs sampled)
memoryUsage: { rss: 107184128,
  heapTotal: 81772544,
  heapUsed: 54756160,
  external: 9284 }
Fastest is trek-router
✨  Done in 65.04s.

Usage

import http from 'http';
import finalhandler from 'finalhandler';
import Router from 'trek-router';

let router = new Router()

// static route
router.add('GET', '/folders/files/bolt.gif', () => {});
// param route
router.add('GET', '/users/:id', () => {});
// match-any route
router.add('GET', '/books/*', () => {});

let result = router.find('GET', '/users/233')
// => [handler, params]
// => [()=>{}, [{name: id, value: 233}]]

let params = {}
if (result[0]) {
  result[1].forEach(param => params[param.name] = param.value)
  // => { id: 233 }
}

// Not Found
let result = router.find('GET', '/photos/233')
// => [handler, params]
// => [undefined, []]

let server = http.createServer(function(req, res) {
  let result = router.find(req.method, req.url);
  if (result) {
    req.params = result[1];
    return result[0](req, res);
  }
  finalhandler(req, res);
});

server.listen(3000)

Badges

NPM version Build Status codecov Dependency status


fundon.me  ·  GitHub @fundon  ·  Twitter @_fundon