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

bajson

v1.2.4

Published

Bourne again json. Huge data friendly & fast json stringifier

Downloads

36

Readme

Bourne again JSON

Supports stringifying for async generators, Node.js object streams and promises.

Big-friendly asynchronous JSON stringifying. Doesn't block the event loop.

Why would I want this?

The main feature is that stringify resolves promises and stringify async iterables, including streams, into arrays on the fly. There's no need to collect their data in an array beforehand, and neither it does that.

bajson also does not monopolize the event loop and emits limited chunks so it doesn't lead to out-of-memory exceptions on large datasets.

It's considerably faster comparing to other non-blocking package bfj, although bfj seems to be more efficient in memory usage. Pick the one that best suits your needs.

Is it fast?

Kind of.

stringify is about 10 times faster than bfj package while still avoiding monopolizing the event loop, though it's still 3-4 times slower than the native implementation of JSON.stringify.

What functions does it implement?

It currently implements stringification, with parsing planned for the future.

import { pipeline } from "node:stream/promises"
import { stringify } from "bajson"

await pipeline(stringify(data), stream)

What kind of async values does it support?

It supports promises and async and sync iterables.

async function createData() {
  yield "text"
  yield 777
}

stringify(createData())
//-> ["text",777]

stringify({ a: Promise.resolve(42), b: createData() })
//-> {"a":42,"b":["text",777]}

Is it compatible with JSON.stringify?

Yes, it accepts the same arguments and returns the same results in all practical scenarios.

However, there is one difference: stringify emits 'null' in cases where JSON.stringify would return undefined. This distinction is due to the fact that stringify cannot return undefined since it always returns a stream.

JSON.stringify(() => {})
// undefined
JSON.stringify({ a: () => {} })
// "{}"
JSON.stringify([() => {}])
// "[null]"
stringify(() => {})
// "null"
stringify({ a: () => {} })
// "{}"
stringify([() => {}])
// "[null]"

How do I write a JSON file?

import { createWriteStream } from "node:fs"
import { pipeline } from "node:stream/promises"
import { stringify } from "bajson"

await pipeline(stringify(data), createWriteStream(path))

How do I response with JSON by http?

import express from "express"
import { pipeline } from "node:stream/promises"
import { stringify } from "bajson"

const app = express()

app.get("/get-json", async (request, response) => {
  response.setHeader("Content-Type", "application/json")
  await pipeline(stringify(data), response)
})

Can I lazily stringify data from MongoDB?

Sure.

import express from "express"
import { pipeline } from "node:stream/promises"
import { stringify } from "bajson"
import { Book } from "@/models/book.model"
import { Author } from "@/models/author.model"
import { Publisher } from "@/models/publisher.model"

const app = express()

app.get("/give-me-it-all", async (request, response) => {
  response.setHeader("Content-Type", "application/json")
  await pipeline(
    stringify({
      books: Book.find().cursor(),
      authors: Author.find().cursor(),
      publishers: Publisher.find().cursor(),
    }),
    response,
  )
  // {
  //   "books": [{ "name": "book1", ... }, { "name": "book2", ... }, ...],
  //   "authors": [{ "name": "author1", ... }, { "name": "author2", ... }, ...],
  //   "publishers": [{ "name": "publisher1", ... }, { "name": "publisher2", ... }, ...]
  // }
})

How do I create a JSON string?

I'm not sure you really want that, but

import { stringify } from "bajson"
import fromAsync from "array-from-async"

await new Blob(await fromAsync(stringify(data))).text()

Why does stringify emit binary chunks instead of strings?

For practical reasons.

There are scenarios where people need to store stringified JSON within a string field. For example:

const data = {
  number: 42,
  anotherData: JSON.stringify({ anotherNumber: 24 }),
}
JSON.stringify(data)

With this module, it transforms into:

const data = {
  number: 42,
  anotherData: stringify({ anotherNumber: 24 }),
}
stringify(data)

If stringify emitted strings, it would be impossible to distinguish between such stringified data and an asynchronous array of strings, which is more commonly used than stringifying an asynchronous array of binaries to JSON.

If the first value of a stream is binary (including zero length), then stringify treats the entire stream as a binary representation of a string; otherwise, it's treated as an array.