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

nyxjson

v0.0.3

Published

⚡ A faster, secure and convenient alternative for JSON.parse

Downloads

36

Readme

cover npm version npm downloads bundle JSDocs License

A faster, secure and convenient alternative for JSON.parse:

Usage

Node.js

Install:

# nyxi
nyxi nyxjson

# pnpm
pnpm add nyxjson

# npm
npm i nyxjson

# yarn
yarn add nyxjson

Import into your Node.js project:

// CommonJS
const nyxjson = require('nyxjson')

// ESM
import nyxjson from 'nyxjson'

Deno

import nyxjson from 'https://deno.land/x/nyxjson/src/index.ts'

console.log(nyxjson('{ "deno": "yay" }'))

Why?

Fast fallback to input if is not string:

// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse()

// undefined
nyxjson()

Fast lookup for known string values:

// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse('TRUE')

// true
nyxjson('TRUE')

Fallback to original value if parse fails (empty or any plain string):

// Uncaught SyntaxError: Unexpected token s in JSON at position 0
JSON.parse('dudel')

// "salam"
nyxjson('dudel')

Avoid prototype pollution:

const input = '{ "user": { "__proto__": { "isAdmin": true } } }'

// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input)

// { user: {} }
nyxjson(input)

Strict Mode

If { strict: true } passed as second argument, nyxjson will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)

// Returns "[foo"
nyxjson('[foo')

// Throws an error
nyxjson('[foo', { strict: true })

Benchmarks

Locally try with nyxr benchmark. Below are esults on Node.js 18.11.0 with MBA M2.

Note nyxjson is sometimes little bit slower than JSON.parse when parsing a valid JSON string mainly because of transform to avoid prototype pollution which can lead to serious security issues if not being sanitized. In the other words, nyxjson is better when input is not always a json string or from untrusted source like request body.

=== Non-string fallback ==
JSON.parse x 5,055,133 ops/sec ±1.49% (89 runs sampled)
nyxjson x 692,414,582 ops/sec ±4.13% (76 runs sampled)
nyxjson (strict) x 798,944,227 ops/sec ±3.01% (86 runs sampled)
sjson:
@hapi/bourne x 4,401,788 ops/sec ±4.41% (79 runs sampled)
Fastest is nyxjson (strict)

=== Known values ==
JSON.parse x 8,512,228 ops/sec ±4.18% (77 runs sampled)
nyxjson x 48,124,313 ops/sec ±4.65% (75 runs sampled)
nyxjson (strict) x 48,440,989 ops/sec ±5.44% (76 runs sampled)
sjson x 6,990,805 ops/sec ±4.13% (78 runs sampled)
@hapi/bourne x 7,895,428 ops/sec ±4.63% (77 runs sampled)
Fastest is nyxjson,nyxjson (strict)

=== Plain string ==
JSON.parse (try-catch) x 64,066 ops/sec ±5.03% (73 runs sampled)
nyxjson x 22,319,224 ops/sec ±5.21% (75 runs sampled)
nyxjson (strict):
sjson (try-catch) x 95,467 ops/sec ±5.90% (69 runs sampled)
@hapi/bourne:
Fastest is nyxjson

=== standard object ==
JSON.parse x 166,726 ops/sec ±4.17% (77 runs sampled)
nyxjson x 117,596 ops/sec ±4.24% (75 runs sampled)
nyxjson (strict) x 117,769 ops/sec ±4.55% (75 runs sampled)
sjson x 150,480 ops/sec ±4.14% (75 runs sampled)
@hapi/bourne x 152,122 ops/sec ±5.23% (72 runs sampled)
Fastest is JSON.parse

=== invalid syntax ==
JSON.parse (try-catch) x 166,171 ops/sec ±5.16% (74 runs sampled)
nyxjson x 106,112 ops/sec ±6.03% (71 runs sampled)
nyxjson (strict) x 123,743 ops/sec ±4.14% (79 runs sampled)
sjson (try-catch) x 147,030 ops/sec ±5.21% (78 runs sampled)
@hapi/bourne x 158,574 ops/sec ±4.31% (75 runs sampled)
Fastest is JSON.parse (try-catch)

License

MIT - Made with 💞