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

bigdecimal.js

v1.3.1

Published

A BigDecimal implementation with native BigInts

Downloads

3,000

Readme

BigDecimal.js

NPM Version NPM Downloads codecov

BigInt based BigDecimal implementation for Node.js 10.4 and above. This implementation is inspired from java BigDecimal class. This implementation is faster than popular big decimal libraries for most operations. See benchmarks results part below for comparison of each operation.

Advantages of this library

  • Faster than other BigDecimal libraries because of native BigInt
  • Simple API that is almost same with Java's BigDecimal
  • No dependencies
  • Well tested
  • Includes type definition file

Disadvantages

  • This library's minified version is about 5 times larger than big.js's minified version. So the library is not small.

Installation

npm install bigdecimal.js

Usage

  • The example usage is given below:
// Single unified constructor for multiple values
const { Big } = require('bigdecimal.js');

// Construct from a string and clone it
const x = Big('1.1111111111111111111111');
const y = new Big(x); // you can also use 'new'

const z = x.add(y);
console.log(z.toString()); // 2.2222222222222222222222

// You can also construct from a number or BigInt:
const u = Big(1.1);
const v = Big(2n);

console.log(u.toString()); // 1.1
console.log(v.toString()); // 2

You can use MathContext to set precision and rounding mode for a specific operation:

const { Big, MC, RoundingMode } = require('bigdecimal.js');

const x = Big('1');
const y = Big('3');

// MC is MathContext constructor that can be used with or without `new`
const res1 = x.divideWithMathContext(y, MC(3)); 
console.log(res1.toString()); // 0.333

const res2 = x.divideWithMathContext(y, new MC(3, RoundingMode.UP));
console.log(res2.toString()); // 0.334

try {
    x.divide(y);
    // throws since full precision is requested but it is not possible
} catch (e) {
    console.log(e); // RangeError: Non-terminating decimal expansion; no exact representable decimal result.
}

Documentation

Testing

  • Install dependencies: npm i
  • Compile: npm run compile
  • Run tests: npm test

Running Benchmarks

There is a benchmark suite that compares

To run the benchmark run npm install and then npm run benchmark.

Benchmark Results

For now, benchmarked against big.js and bigdecimal.

  • Test Machine:

    • M1 2021 MacBook Air
    • 16 GB Ram
    • MacOS Sonoma 14.2.1
  • Update Date: January 28th 2024

  • Library versions used:

    • big.js 6.2.1
    • (this library) bigdecimal.js 1.3.1
    • bigdecimal 0.6.1
    • bignumber.js: 9.1.2
    • decimal.js:10.4.3
  • Each operation is run with fixed set of decimal numbers composed of both simple and complex numbers.

  • Micro benchmark framework used is benchmark. Check out benchmarks folder for source code of benchmarks.

  • For now, benchmarked the following operations, more operations will be added later.

  • Operations per second(op/s):

| Operation | Bigdecimal.js | Big.js | BigNumber.js | decimal.js | GWTBased | | --- | --- | --- | --- | --- | --- | | Constructor | 43,962 ( - ) | 38,238 (-13%) | 42,337 (-4%) | 42,355 (-4%) | 2,818 (-94%) | | Add | 80,569 ( - ) | 18,406 (-77%) | 100,734 (+25%) | 59,815 (-26%) | 90 (-100%) | | Subtract | 73,518 ( - ) | 18,265 (-75%) | 96,022 (+31%) | 57,130 (-22%) | 89 (-100%) | | Multiply | 493,291 ( - ) | 33,422 (-93%) | 26,810 (-95%) | 79,995 (-84%) | 2,609 (-99%) | | Divide | 15,341 ( - ) | 1,129 (-93%) | 11,721 (-24%) | 13,301 (-13%) | 645 (-96%) | | Remainder | 9,362 ( - ) | 3,816 (-59%) | 13,470 (+44%) | 21,952 (+134%) | 2,445 (-74%) | | Positive pow | 27,403 ( - ) | 25 (-100%) | 113 (-100%) | 3,535 (-87%) | 6 (-100%) | | Negative pow | 4,863 ( - ) | 21 (-100%) | 109 (-98%) | 1,970 (-59%) | 264 (-95%) | | Abs | 782,251 ( - ) | 1,424,376 (+82%) | 917,526 (+17%) | 358,678 (-54%) | 14,132 (-98%) | | Compare | 546,243 ( - ) | 1,216,388 (+123%) | 783,432 (+43%) | 417,873 (-24%) | 990,187 (+81%) |