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-bignum

v0.0.3

Published

Node.js JSON replacement which handles 64-bit integers and arbitrary-precision decimals.

Downloads

750,461

Readme

json-bignum

Node.js JSON replacement which handles 64-bit integers and arbitrary-precision decimals. It is a modified version of Douglas Crockford's JSON library. Although it can handle 64-bit integers and arbitrary-precision decimals, it is slower than the built-in JSON functions.

Install

$ npm install json-bignum

Usage

parse()

var bignumJSON = require('json-bignum');

var obj = bignumJSON.parse('{ "decimal": -9223372036854775807.4237482374983253298159 }');

stringify()

var bignumJSON = require('json-bignum');

var obj = {
    bigint: new bignumJSON.BigNumber('92233720368547758074237482374983253298159'),
    decimal: new bignumJSON.BigNumber('-9223372036854775807.4237482374983253298159'),
};

console.log(bignumJSON.stringify(obj));

BigNumber

The BigNumber class simply stores the number as a string. It does not support arithmetic, but if you need that here are some excellent libraries.

  • BigDecimal.js: a literal port of Java's BigInteger and BigDecimal classes.
  • bigint: Big integer arithmetic using GMP.
  • bignum: Big integer arithmetic using OpenSSL.
// example using BigDecimal.js

var bignumJSON = require('json-bignum');
var bigdecimal = require('bigdecimal');

var jsonStr = '{"normal":-922337203.234,"big":-9223372036854775807.4237482374983253298159}';
var jsonObj = bignumJSON.parse(jsonStr);

var a = new bigdecimal.BigDecimal(jsonObj.normal.toString());
var b = new bigdecimal.BigDecimal(jsonObj.big.toString());
var sum = a.add(b);

jsonObj.sum = new bignumJSON.BigNumber(sum.toString());

console.log(bignumJSON.stringify(jsonObj));

Caveats

It is not recommended to mix calls to JSON and bignumJSON. For example, JSON.stringify() does not know how to parse BigNumber.

Benchmark

Below shows the result of the benchmark on my machine.

$ node benchmark.js
10000 calls of JSON.parse():                                   26.746847 ms
10000 calls of JSON.stringify():                               20.824071 ms
10000 calls of bignumJSON.parse() with bignums in JSON:        221.945307 ms
10000 calls of bignumJSON.parse() without bignums in JSON:     150.626292 ms
10000 calls of bignumJSON.stringify() with bignums in JSON:    64.166056 ms
10000 calls of bignumJSON.stringify() without bignums in JSON: 61.860016 ms