JSPM

  • Created
  • Published
  • Downloads 16600
  • Score
    100M100P100Q162713F
  • License BSD-2-Clause

fast-async/await transformer Babel plugin

Package Exports

  • fast-async

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fast-async) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

fast-async

'fast-async' is a Babel v6.x.x plugin that implements the ES7 keywords async and await using syntax transformation at compile-time rather than generators.

The main reason for using 'fast-async' as opposed to Babel's default implementation of async/await is performance (https://github.com/MatAtBread/nodent#performance) - it's 3-4 times faster in a browser/node, and as much as 10 times faster on a mobile browsers, mainly due to avoiding generators (and therefore regenerator).

There's a simple test (that just makes sure the plugin works and generates code that runs). More complete test coverage is included with nodent.

Because Babel parses the code, the ES7 extensions possible with nodent (await anywhere, async return and async throw) are not supported, however full implementation of async function containing await expressions is implemented.

For Babel v5.x.x install fast-async@1.0.3

Install

npm install fast-async

Usage

Just include the plugin to the babel options. Minimal .babelrc example:

{
    "plugins": ["fast-async"]
}

That's all. Neither babel-plugin-transform-runtime nor babel-polyfill required.

With options:

{
    "plugins": [
        ["fast-async", {
            "env": {
                "augmentObject": false,
                "dontMapStackTraces": false,
                "asyncStackTrace": false,
                "dontInstallRequireHook": false
            },
            "compiler": {
                "promises": true,
                "generators": false
            },
            "runtimePattern":null
        }]
    ]
}

Test

From the installation directory (e.g. node_modules/fast-async):

npm test

Options

The plugin accepts the following options object, which itself is optional, as are all members. These are based on the options in nodent, but since much of the parsing is done by Babel some are unused.

env:{
    log:function(string),        // Supplied routine to emit transformation warnings. Default: console.log
    augmentObject:false,         // Add the nodent utilities asyncify() and isThenable() to Object.prototype
    dontMapStackTraces:false,    // Don't install the stack trace hook that maps line numbers
    asyncStackTrace:false,       // Provide async stack traces
    dontInstallRequireHook:false // Don't transform all JS files as they are loaded into node
},
compiler:{
    promises:true,    // Use nodent's "Promises" mode. Set to false if your execution environment does not support Promises.
    generators:false  // Transform into 'Generators'.
},
runtimePattern:null   // See below

For more information on the compiler options, see ES7 and Promises in the nodent documentation.

runtimePattern By default, fast-async will put the nodent runtime into every file it compiles. If your project is made up of more than one file, the constant redefinition of the runtime is a waste of time and space. You can specify that you want the runtime in particular file(s) by setting the 'runtimePattern' to a regular expression (in quotes). Only files that match the regular expression will have the runtime defined (which is global, so you only need it once). For example:

  "babel": {
    "plugins": [
      "syntax-async-functions",
      ["fast-async",{
        "runtimePattern":"test-input\\.js"
      }]
    ]
  }

Alternatively, if you set runtimePattern to "directive", the statement "use runtime-nodent"; will be replaced with the runtime during compilation.

Online performance checkers:

  • nodent 632ms (and shave off another 100ms by selecting 'Pure ES5' mode)
  • babel 1877ms - 3x slower
  • traceur 2488ms - 4x slower