• Stars
    star
    436
  • Rank 99,877 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

faster.js is a Babel plugin that compiles idiomatic Javascript to faster, micro-optimized Javascript.

NPM Version Build Status

faster.js

faster.js is a Babel plugin that compiles idiomatic Javascript to faster, micro-optimized Javascript.

Read the blog post on faster.js!

Installation

Setup Babel for your project if you haven't already. Then install faster.js:

npm install --save-dev faster.js

Usage

.babelrc
{
  "plugins": ["faster.js"]
}
Babel CLI
babel-cli --plugins faster.js script.js
webpack.config.js (Webpack 4)
module: {
  rules: [{
    test: /\.js$/,
    exclude: /(node_modules)/,
    use: {
      loader: 'babel-loader',
      options: {
        plugins: [require('faster.js')]
      }
    }
  }]
}

What faster.js does

faster.js rewrites common Array method calls to faster code that does the same thing (usually - see When NOT to use faster.js). This results in performance boosts (especially on code that relies heavily on Array methods) while maintaining code readability, but comes at the cost of a slightly larger bundle size. If having a small Javascript bundle size is much more important for you than performance is, you should not use faster.js.

Supported Array methods

faster.js will rewrite the following Array methods when possible:

  • .every()
  • .filter()
  • .forEach()
  • .map()
  • .reduce()
  • .reduceRight()
  • .some()

Demo

faster.js Demo Screenshot

Try it yourself: https://fasterjs-demo.victorzhou.com

Demo Github repo: https://github.com/vzhou842/faster.js-demo

⚠️ When NOT to use faster.js

faster.js makes two critical assumptions that MUST be true about your codebase:

1. Sparse Arrays are never used.

Code compiled with faster.js may produce incorrect results when run on sparse arrays.

2. Restricted methods are only ever called on native Javascript arrays:

faster.js assumes any restricted method call is done on a native Javascript array. Any new classes you write should not include methods with restricted names.

Restricted method names are the names of methods that faster.js will attempt to rewrite - see Supported Array methods.

// OK
const a = [1, 2, 3].map(e => 2 * e);

// BAD
class Foo {
  constructor(map) {
    this._map = map;
  }
  map() {
    return this._map;
  }
}
const f = new Foo({});
const map = f.map(); // .map() is a restricted method

How faster.js works

faster.js exploits the fact that native Javascript Array methods are slowed down by having to support seldom-used edge cases like sparse arrays. Assuming no sparse arrays, there are often simple ways to rewrite common Array methods to improve performance.

Example: Array.prototype.forEach()

// Original code
const arr = [1, 2, 3];
const results = arr.map(e => 2 * e);

roughly compiles to

// Compiled with faster.js
const arr = [1, 2, 3];
const results = new Array(arr.length);
const _f = (e => 2 * e);
for (let _i = 0; _i < arr.length; _i++) {
  results[_i] = _f(arr[_i], _i, arr);
}

Benchmarks

Example benchmark output (condensed)

$ npm run bench

  array-every large
    ✓ native x 2,255,548 ops/sec ±0.46% (57 runs sampled)
    ✓ faster.js x 10,786,892 ops/sec ±1.25% (56 runs sampled)
faster.js is 378.2% faster (0.351μs) than native

  array-filter large
    ✓ native x 169,237 ops/sec ±1.42% (55 runs sampled)
    ✓ faster.js x 1,110,629 ops/sec ±1.10% (59 runs sampled)
faster.js is 556.3% faster (5.008μs) than native

  array-forEach large
    ✓ native x 61,097 ops/sec ±3.66% (43 runs sampled)
    ✓ faster.js x 200,459 ops/sec ±0.52% (55 runs sampled)
faster.js is 228.1% faster (11.379μs) than native

  array-map large
    ✓ native x 179,800 ops/sec ±1.00% (58 runs sampled)
    ✓ faster.js x 1,706,593 ops/sec ±0.25% (56 runs sampled)
faster.js is 849.2% faster (4.976μs) than native

  array-reduce large
    ✓ native x 200,425 ops/sec ±1.01% (55 runs sampled)
    ✓ faster.js x 1,694,350 ops/sec ±1.52% (55 runs sampled)
faster.js is 745.4% faster (4.399μs) than native

  array-reduceRight large
    ✓ native x 49,784 ops/sec ±0.38% (58 runs sampled)
    ✓ faster.js x 1,756,352 ops/sec ±0.99% (59 runs sampled)
faster.js is 3428.0% faster (19.517μs) than native

  array-some large
    ✓ native x 2,968,367 ops/sec ±0.56% (56 runs sampled)
    ✓ faster.js x 11,591,773 ops/sec ±1.29% (54 runs sampled)
faster.js is 290.5% faster (0.251μs) than native

The benchmark example above was run on Node 8. Later versions of Node include improvements / optimizations that may make some features in faster.js obsolete. View full benchmark examples here: Node 8, Node 10, Node 12.

FAQ

What is a sparse array?

Sparse arrays are arrays that contain holes or empty slots.

const sparse1 = [0, , 1]; // a sparse array literal
console.log(sparse1.length); // 3

const sparse2 = [];
sparse2[5] = 0; // sparse2 is now a sparse array
console.log(sparse2.length); // 6

It is generally recommended to avoid using sparse arrays.

More Repositories

1

profanity-check

A fast, robust Python library to check for offensive language in strings.
Python
630
star
2

example-.io-game

An example multiplayer (.io) web game.
JavaScript
441
star
3

cnn-from-scratch

A Convolutional Neural Network implemented from scratch (using only numpy) in Python.
Python
281
star
4

neural-network-from-scratch

A Neural Network implemented from scratch (using only numpy) in Python.
Python
208
star
5

victorzhou.com

My personal website + blog. Built with Gatsby.js.
JavaScript
154
star
6

rnn-from-scratch

A Recurrent Neural Network implemented from scratch (using only numpy) in Python.
Python
84
star
7

gatsby-plugin-optimize-svgs

A Gatsby Plugin to minify SVGs output to the filesystem during the build.
JavaScript
37
star
8

generals.io-Node.js-Bot-example

An example Node.js bot for generals.io. Learn more at http://dev.generals.io/api#tutorial
JavaScript
32
star
9

easy-VQA

The Easy Visual Question Answering dataset.
Python
32
star
10

definitely-secure-bank

A definitely (read: not) secure online banking site. Built for demo purposes as an example of common security vulnerabilities / what NOT to do.
JavaScript
24
star
11

easy-VQA-keras

A Keras implementation of VQA using the easy-VQA dataset.
Python
22
star
12

generals.io-Replay-Utils

Official utils for processing generals.io replays. Learn more at http://dev.generals.io/replays
JavaScript
20
star
13

easy-VQA-demo

A Web-based Javascript Demo of an easy-VQA model.
JavaScript
13
star
14

OutsideChat-iOS

A mesh network peer-to-peer communication platform, built at Outside Hacks 2015.
Objective-C
5
star
15

gatsby-remark-code-headers

Add headers (like filenames) to code blocks for Gatsby.js.
JavaScript
3
star
16

faster.js-demo

An demo of the faster.js Babel plugin.
JavaScript
2
star
17

COS-333-Project

Robin, a project for COS 333.
Swift
2
star
18

DARKHOUSE

A game inspired by Nintendo's "Luigi's Mansion"
JavaScript
1
star
19

generals-bot-cpp

A C++ generals.io bot.
C++
1
star
20

Cloak

Hiding information in photos. HackPrinceton 2015 - Best Privacy & Security Hacks, First Place.
Objective-C
1
star
21

Robin

An anonymous location-based social mobile app that aims to connect people via sharing. Built for COS 333.
1
star
22

victorzhou.com-Old

My old personal website. See the new one at https://github.com/vzhou842/victorzhou.com
JavaScript
1
star
23

PEI-visualizer

A Physical, Emotional, and Intellectual health visualizer.
JavaScript
1
star
24

Falling-Dots-iOS

iOS App for Falling Dots (made by Victor Zhou).
Objective-C
1
star