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

fast-toml

v0.5.4

Published

Fast and light parser for TOML 0.5, an alternative to JSON or YAML file formats.

Downloads

1,416

Readme

Fast TOML Parser for Node.js

fast-toml is the fastest and lightest Javascript parser for TOML files (see benchmarks below).

TOML stands for Tom's Obvious and Minimal Language and it is an awesome language for your configuration files, better than JSON and YAML on many aspects. Learn here what is TOML and how to use it (it's definitely worth the ten minutes learning).

Usage

First, install fast-toml : npm i fast-toml.

Then, let's suppose we have the following TOML file :

# myFile.toml
title = 'Hey universe'

[soundOptions]
volume = 68
soundName = 'Hey universe'
file = 'sounds/hey-universe.mp3'

We read the file and transform it into a javascript object this way :

const TOML = require('fast-toml')
const fs = require('fs')

const tomlString = String(fs.readFileSync('myFile.toml'))
const data = TOML.parse(tomlString)
console.log(data.title)  // 'Hey universe'
console.log(data.soundOptions.volume)  // 68

TOML.parseFile

If you want to read from a file, you can directly use the TOML.parseFile or TOML.parseFileSync functions :

const TOML = require('fast-toml')

// async / await (any error will be thrown)
const data = await TOML.parseFile('myFile.toml')
console.log(data)

// sync (any error will be thrown)
const data = TOML.parseFileSync('myFile.toml')
console.log(data)

// promise (we catch errors in a callback)
TOML.parseFile('myFile.toml')
.then(data => console.log(data))
.catch(err => console.error(err))

Javascript template strings

You also can use the parser with Javascript template strings :

const TOML = require('fast-toml')
const data = TOML `
  title = 'Hey universe'

  [soundOptions]
  volume = 68
  soundName = 'Hey universe'
  file = 'sounds/hey-universe.mp3'
`
console.log(data.title)  // 'Hey universe'
console.log(data.soundOptions.volume)  // 68

Using in browser

You can download the browser version of fast-toml here.

Just add the file to your project and require it with a script tag. You can then use the globally defined TOML object.

Speed and size comparison with other parsers

Here is the comparison between fast-toml and the other 0.5.0-compliant TOML parsers for Javascript :

| | fast-toml | Iarna's toml | j-toml | Bombadil | |-----------------------------------------------------------------|------------|--------------|----------------------|----------| | Require | 2.375 | 14.720 | 5.969 | 196.741 | | First round | 9.489 | 13.911 | 12.267 | 69.970 | | One-use (require+first round) | 11.864 | 28.631 | 18.236 | 266.711 | | Warm round | 1.483 | 7.275 | 1.420 | 34.878 | | Hot round | 0.501 | 0.604 | 0.627 | 6.639 | | Package size | 12.7 ko | 93.1 ko | 261 ko | +3000 ko |

(All time values are milliseconds)

The comparison has been made in a Node 11.2.0 environment with this medium-size sample TOML file, which covers about all the different ways to use TOML.

The comparison has been made in three rounds because of the way Javascript works :

  • For the first round, the Javascript engine has done no compilation yet. The functions are directly interpreted when evaluated.
  • After a fisrt round, the Javascript engine will do some light compilation called warming. That's why the second call is faster than the first.
  • If a function is called many times, the Javascript engine will do hot compilation optimisations so that the function runs super-fast.

Bombadil is so big and slow compared to others parsers because it uses a third-party library (Chevrotain) - even though Chevrotain is describing itself as 'blazing fast' (as everyone does nowadays :p).

fast-toml is also robust. Errors are prettily handled, giving you clear informations about bad syntaxes.

Note about package size

In actual NodeJs package ecosystem, your imported libraries can grow very fast in size. Because they import other libraries themselves, which also import their own libraries, etc... Furthermore, most libraries carry a lot of stuff you absolutely don't need.

The package size of fast-toml is so small because it follows this principle :

  • the source code (+ your tests + dev dependencies), should be on github/gitlab or similar. Users who want to collaborate on your project will git clone it (not npm install it).
  • your npm package should only contain the useful code (production-ready). When you use a third party library, you expect it has been battle-tested and you don't need the development version. If you have several versions of your code (CommonJS, ESmodules, browser), you should have one package for each of these versions.

Permissivity

fast-toml is a bit more permissive than his brothers and sisters. Especially :

  • line breaks make commas optionals in inline arrays and tables,
  • unrecognized value types will be treated as strings (which makes quotes optional),
  • if you define a table and add an element with a value but no key, the table will be considered as an array.

These small tweaks make the TOML language even more comfortable to read and write, but be aware they are not standard.