Git Product home page Git Product logo

tree-shaking-default-params's Introduction

Tree-Shaking Default Parameters

This demo tests how various bundlers handle Libauth's WebAssembly crypto implementations (ripemd160, sha1, sha256, sha512, and secp256k1).

As a pure ESM library, Libauth v2 can asynchronously instantiate each WASM implementation internally, exporting simple interfaces that behave like collections of JS-only functions (with better performance).

Many of Libauth's exported functions also use one of these built-in WASM instances as a default parameter. For example, decodeBase58Address has the definition:

export const decodeBase58Address = (
  address: string,
  sha256: { hash: (input: Uint8Array) => Uint8Array } = internalSha256
) => {
  // ...
};

Most applications can call decodeBase58Address(address) to automatically use the default, WASM-based sha256 implementation (internalSha256). See src/app-default.js.

Applications that already have another sha256 implementation can provide that implementation as the second parameter: decodeBase58Address(address, mySha256Implementation). In this case, the default parameter (internalSha256) is dead code and should be possible to eliminate from the application's bundle (commonly called tree shaking in the JavaScript ecosystem). See src/app-shakable.js.

Bundler Support

This repo tests dead-code elimination of unused default parameters as used in Libauth (with asynchronously instantiated WASM implementations): src/app-shakable.js.

Currently, dead-code elimination of unused default parameters is only supported by Rollup.

Bundler Support Issue PR
esbuild ❌ (example) evanw/esbuild#2185 -
parcel ❌ (example) parcel-bundler/parcel#7961 -
rollup ⏳ (example) rollup/rollup#4466 rollup/rollup#4498
terser ❌ (example) terser/terser#1199 -
webpack ❌ (example) webpack/webpack#15671 -

This simpler example may also be useful for quickly testing support in bundlers:

package.json:

{
  "type": "module"
}

module.js:

// should not appear in bundle:
const internalAdd = (a, b) => (a === '0' ? b : a + b);

export const addAndLog = (a, b, impl = internalAdd) => console.log(impl(a, b));

app.js:

import { addAndLog } from './module.js';

const myAdd = (a, b) => a + b;

addAndLog(1, 2, myAdd); // => 3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.