Package detail

turbo-combine-reducers

aduth109.7kMIT1.0.2

Speed-optimized drop-in replacement for Redux's combineReducers

redux, reducer, reducers

readme

Turbo Combine Reducers

Build Status

Drop-in replacement for Redux's combineReducers, optimized for speed and bundle size.

Applying a technique of partial evaluation by pre-compiling a reducer updater function, it achieves upwards of 82x improved performance for an assumed typical real-world scenario (unchanging state for a medium-size object). See Benchmarks for specific performance metrics.

Turbo Combine Reducers has no dependencies, and weighs in at 260 bytes minified and gzipped.

Installation

Using npm as a package manager:

npm install turbo-combine-reducers

Otherwise, download a pre-built copy from unpkg:

https://unpkg.com/turbo-combine-reducers/dist/turbo-combine-reducers.min.js

Usage

As an imported package, the default export is the combineReducers function. If using the browser-ready distributable from unpkg, the same function is available at the window.turboCombineReducers global.

import combineReducers from 'turbo-combine-reducers';
// Or:
// var combineReducers = window.turboCombineReducers;

const reducer = combineReducers( {
    count( state = 0, action ) {
        // ...
    },
} );

As it is intended to serve as a drop-in replacement, refer to the documentation of Redux's combineReducers for usage instructions.

Benchmarks

The following benchmarks are performed in Node 10.12.0 on a MacBook Pro (Late 2016), 2.9 GHz Intel Core i7.

turbo-combine-reducers - unchanging (4 properties) x 120,638,598 ops/sec ±0.37% (87 runs sampled)
turbo-combine-reducers - unchanging (20 properties) x 23,740,676 ops/sec ±1.20% (90 runs sampled)
turbo-combine-reducers - changing (4 properties) x 40,028,499 ops/sec ±0.82% (90 runs sampled)
turbo-combine-reducers - changing (20 properties) x 23,338,161 ops/sec ±0.86% (88 runs sampled)
redux - unchanging (4 properties) x 1,480,178 ops/sec ±0.42% (93 runs sampled)
redux - unchanging (20 properties) x 594,068 ops/sec ±0.39% (90 runs sampled)
redux - changing (4 properties) x 1,390,904 ops/sec ±0.28% (92 runs sampled)
redux - changing (20 properties) x 589,844 ops/sec ±0.47% (92 runs sampled)

License

Copyright 2018 Andrew Duthie

Released under the MIT License.

changelog

1.0.2 (2018-10-20)

Bug Fixes

  • Further improve hardiness of key escaping leveraging JSON.stringify quoting behavior, avoiding remaining potential for runtime errors.

1.0.1 (2018-10-20)

Bug Fixes

  • Protect against escaped evil escaping.

A note on security: Turbo Combine Reducers uses new Function dynamic function evaluation (i.e. an eval equivalent) to pre-compile the state value reducer. The risk surface area is limited to reducer property names. Most applications will never (and should never) include a dynamic, user-input value as a reducer key and thus would not be exposed to any risk, including in prior releases. The changes in this release more aggressively sanitize reducer keys to offer protection even in the limited use-case where an unsafe user-input reducer key would be intended to be used.

1.0.0 (2018-10-20)

  • Initial release