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

babel-plugin-transform-hoist-nested-functions

v1.2.0

Published

Babel plugin to hoist nested functions

Downloads

22

Readme

babel-plugin-transform-hoist-nested-functions

Greenkeeper badge circle npm coverage

semantic release js-semistandard-style MIT License

Babel plugin to hoist nested functions to the outermost scope possible without changing their contract.

Examples

Example 1 - basic hoisting

In

function renderApp () {
  return renderStateContainer(
    ({value}) => renderValue(value)
  );
}

Out

var _hoistedAnonymousFunc = ({ value }) => renderValue(value);

function renderApp () {
  return renderStateContainer(_hoistedAnonymousFunc);
}

Example 2 - nested method hoisting

To enable this transformation, pass the methods: true option to the plugin (see below). The output code depends on the ES2015 Symbol feature and the stage 2 class properties proposal. You will most likely want to run babel-plugin-transform-class-properties after transform-hoist-nested-function.

In

class Foo {
  bar () {
    return () => this;
  }
}

Out

const _hoistedMethod = new Symbol("_hoistedMethod"),

class Foo {
  [_hoistedMethod] = () => this;

  bar() {
    return this[_hoistedMethod];
  }
}

Motivation

Patterns like React "render callbacks", that make heavy use of nested functions, incur the nonzero runtime cost of creating those functions over and over. JavaScript engines don't always optimize this cost away.

To mitigate this cost, this plugin moves functions out of inner scopes wherever possible. A function can be moved up through any scope that it does not reference explicitly. This is somewhat analogous to what babel-plugin-transform-react-constant-elements does (and in fact some of the same Babel machinery is applied).

Caveats

Experimental

This is a new, experimental plugin. Expect changes (adhering religiously to semver), and please, please, PLEASE test and benchmark your code very thoroughly before using this in anything important.

Not 100% transparent

While the plugin aims not to change the behavior of hoisted functions, the fact that they are reused rather than recreated does have some visible consequences.

Consider the following code:

function factory () {
  return function foo () {}; // foo() will be hoisted right above factory()
}
factory() === factory(); // ⬅ value depends on whether foo() is hoisted

That last expression evaluates to false in plain JavaScript, but is true if foo() has been hoisted.

More fundamentally, references to hoisted inner functions are allowed to escape their enclosing scopes. You should determine whether this is appropriate for your code before using this plugin.

Benchmarks

Here are benchmark results from the latest successful build on master using Node v4 (make your own with npm run benchmark). The benchmark code is here - each file exports a single function that is repeatedly run and timed by [Benchmark.js] (https://benchmarkjs.com).

From these preliminary results, it appears that hoisting functions this way can in fact improve performance, at least in principle; but the benefit may not always be significant.

Installation

$ npm install --save-dev babel-plugin-transform-hoist-nested-functions

Usage

Via .babelrc (Recommended)

.babelrc

// without options
{
  "plugins": ["transform-hoist-nested-functions"]
}

// with options
// NOTE: transform-class-properties is required in order to run the code
{
  "plugins": [
    ["transform-hoist-nested-functions", {
      "methods": true
    }],
    "transform-class-properties"
  ]
}

Via CLI

$ babel --plugins transform-hoist-nested-functions script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["transform-hoist-nested-functions"]
});

Development

Use npm v3: npm install -g npm@3

git clone https://github.com/motiz88/babel-plugin-transform-hoist-nested-functions
cd babel-plugin-transform-hoist-nested-functions
npm install
# ... hackity hack hack ...
npm run test:local # Including tests (mocha), code coverage (nyc), code style (eslint), type checks
                   # (flow) and benchmarks.

See package.json for more dev scripts you can use.

Contributing

PRs are very welcome. Please make sure that test:local passes on your branch.