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

json-index

v1.1.0

Published

JSON stream index to operate over large files

Downloads

2

Readme

json-index

Very simple and fast index for large JSON files.

It reads a collection of objects from a JSON file and creates indexes for properties. Once the index is ready, it allows to query the index by properties and it lazily reads the original file when an entry is required.

IMPORTANT: it doesn't support to filter objects, it assumes the file is a JSON with a plain collection of objects, and it only indexes the first-level objects.

Installation

Install from npm. It requires Node >= 4.

npm install json-index --save

Usage

const DATA_FILE = "data/large.json";
const INDEX_DIR = "data/index/";

var index = require("json-index")(DATA_FILE, INDEX_DIR, {
  indexes: ["field1", "field2", "field3"],
  readBufferSize: 1024 * 1024 * 2.5,
  parseBufferSize: 1024 * 1024 * 512
});

index.load().then(() => {
  index.query({
    field1: "value"
  }).then(items => {
    console.log("Total matches: ", items.length);
    index.close();
  }).catch(err => console.log("Error creating/reading index:", err));
});

Reference

index.load()

Loads the index. Creates the index if it doesn't exist. Once loaded, the index can be queried.

index.close()

Closes the index and releases any opened file handle.

index.query(params)

Queries any indexed fields. The keys in the params object are the indexes names. Values are the indexed data to match.

It returns a promise to an EntryIterator object.

EntryIterator

The entry iterator class represents a lazy result set. It reads the data file only if items are required. It is a syncronous iterator, so any operation that cause it to load data into memory is blocking. It implements ES6 iterator, so it can be used with for ... of. For instance:

index.query({
  field1: "value"
}).then(items => {
  for (var item of items) {
    console.log("Item id:", item.id);
  }
});

EntryIterator#every(callback)

Same as Array.prototype.every.

EntryIterator#filter(callback)

Same as Array.prototype.filter.

EntryIterator#get(index)

Returns the specified item.

EntryIterator#forEach(callback)

Same as Array.prototype.forEach.

EntryIterator#length

Returns the number of items in the iterator.

EntryIterator#map(callback)

Same as Array.prototype.map.

EntryIterator#reduce(callback, initValue)

Same as Array.prototype.reduce.

EntryIterator#slice(callback)

Same as Array.prototype.slice.

EntryIterator#some(callback)

Same as Array.prototype.some.

Theory and performance

Disk read is slow. It is not possible to scale reading from the file system. I recommend you to take a look at this great article in order to realize that disk IO cannot be improved by software, even trying multi- threading. The only scenario in which it is possible to take advantage of parallel IO is when there's data processing while another thread is reading from disk.

On my rough benchmarks, the average read speed of my laptop is 49.2 MB/s. You can try guessing your read speed writing and reading by using dd command:

This command creates a 1.6GB file and reports the average write speed:

$ dd if=/dev/zero of=speedtest bs=64k count=25600 conv=fdatasync 25600+0 records in 25600+0 records out 1677721600 bytes (1.7 GB) copied, 20.6269 s, 81.3 MB/s

This command copies the previous file to a new one and reports the average read speed:

$ dd if=speedtest of=copytest bs=64k conv=fdatasync 25600+0 records in 25600+0 records out 1677721600 bytes (1.7 GB) copied, 34.7155 s, 48.3 MB/s

The average read speed is 48.3 MB/s, it means any application running on user space cannot go beyond this rate. It is the same with this index. Once created, queries are fast, but traversing a large resultset will cause buffered reads.

How it works

Short answer: buffering. The index has a configurable memory buffer for parsing and creating the index, and another buffer to read index entries. It reads the data file from top to bottom loading data into the buffer asynchronously, and in the meantime it processes the data reading a stream of json objects.

The parsing buffer size can be configured with the parseBufferSize option. As data is loaded and processed in parallel, the memory usage always doubles the parseBufferSize value.

The read index buffer keeps a range of data loaded into memory when a single entry is required. Queries are lazy, the data file content is loaded into memory only if an item is required, and only if the item does not exist in the buffer. So, it is faster to traverse a collection if queries return items that are placed consecutively in the data file.

Beyond the data buffers, it has a very small memory footprint. The index size in memory is negligible. The V8 engine represents objects in very efficient structures. I recommend to take a look at this technical documentation about Node and V8 architecture.

Regarding indexes, it creates a single index for each indexed field. Indexes are stored in the file system as JSON files.