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

@planetarium/bencodex

v0.2.2

Published

An alternative take on implementing Bencodex in TypeScript/JavaScript

Downloads

1,328

Readme

Bencodex.js

deno.land/x/bencodex status npm status CI status Coverage status

This library is an alternative take on implementing Bencodex in JavaScript.

It focused to address the following problems from the existing JavaScript implementation(s):

  • No Node.js-only APIs: It does not depend on Node.js-only APIs such as Buffer. Usable with web browsers, Deno, and Node.js.

  • Static-time and runtime type safety: It is fully written in TypeScript, and promise us, we use any nowhere, and it never trusts any data from external sources thoughtlessly. Instead, it tries to parse data and validate them whenever it read data from external sources. It's not only written in TypeScript, it also has runtime checks here and there so that you could use it in vanilla JavaScript without fear.

  • Well-tested: Passing only the Bencodex spec test suite is not enough to guarantee the whole functionality of a library, as every library has their own part to interpret data in JavaScript (how data should look like in JavaScript values, where data are read from and written into, whether data are streamed or given in a single big chunk, etc). It has its own unit tests besides spec test suite, and we've done the best to reach near-100% test coverage.

  • Comprehensive docs: Every non-trivial library needs the three types of docs: tutorials for getting stated, in-depth manuals for common scenarios, and complete API references with concise examples. It has all of them!

  • Reducing unnecessary memcpy: Instead of creating chunks of Uint8Arrays and then allocating a large buffer to concatenate the whole data once again, it allocates only the necessary buffer only once. It is not just memory-efficient, also time-efficient as memcpy quite takes time.

  • Proper binary keys: It does not simply depend on Map, which compares its Uint8Array keys by reference equality. Instead, it implements its own proper dictionary which compares Uint8Array by content equality.

Distributed under LGPL 2.1 or later.

Getting started

See also the complete API references as well.

Deno

It's available on deno.land/x/bencodex:

import { decode, encode } from "https://deno.land/x/bencodex/mod.ts";

const value = new Map([
  ["foo", true],
  ["bar", null],
  ["baz", new Uint8Array([0x73, 0x70, 0x61, 0x6d])],
  [
    new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]),
    [
      123456n,
      "Unicode string",
      false,
    ],
  ],
]);
const encoded = encode(value);
const decoded = decode(encoded);

Node.js

Add @planetarium/bencodex to your dependencies using your favorite package manager:

npm install @planetarium/bencodex

The API usage is equivalent to the example above for Deno, except that you need to import things from "@planetarium/bencodex" instead of the deno.land/x/ URL:

import { decode, encode } from "@planetarium/bencodex";

Types

| Bencodex | TypeScript (Value) | |------------|-------------------------------| | Null | null | | Boolean | boolean | | Integer | bigint (not number) | | Binary | Uint8Array | | Text | string | | List | Value[] | | Dictionary | Dictionary |

Benchmarks

$ deno bench
cpu: 12th Gen Intel(R) Core(TM) i5-1235U
runtime: deno 1.30.3 (x86_64-unknown-linux-gnu)

file:///home/dahlia/src/planetarium/bencodex.js/bench/comparison.bench.ts
benchmark                        time (avg)             (min … max)       p75       p99      p995
------------------------------------------------------------------- -----------------------------
encoding (bencodex.js)        28.96 µs/iter   (17.24 µs … 10.43 ms)  22.25 µs  117.5 µs 221.98 µs
encoding (disjukr/bencodex)   66.01 µs/iter    (39.36 µs … 4.38 ms)  55.58 µs  215.2 µs 313.14 µs

summary
  encoding (bencodex.js)
   2.28x faster than encoding (disjukr/bencodex)

decoding (bencodex.js)         7.73 µs/iter    (6.35 µs … 16.97 ms)   6.92 µs  17.39 µs  30.93 µs
decoding (disjukr/bencodex)   45.76 µs/iter     (31.2 µs … 5.08 ms)  46.35 µs 135.29 µs 185.85 µs

summary
  decoding (bencodex.js)
   5.92x faster than decoding (disjukr/bencodex)

You can run the benchmarks by deno bench.