From 3ce001a4f6f8a9da47c458df3b1bdf95ebda1ec9 Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Tue, 11 Aug 2015 23:53:05 -0700 Subject: [PATCH] Replace lodash curry with our own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lodash's curry is great and does a lot, but we don't need it all. This is quite a bit faster. Currently, it does away with placeholder support. updeep curry partial call x 9,951,410 ops/sec ±1.61% (62 runs sampled) lodash curry partial call x 988,949 ops/sec ±1.36% (62 runs sampled) --- lib/if.js | 6 ++++++ lib/index.js | 5 ++--- lib/is.js | 2 +- lib/update.js | 2 +- lib/updateIn.js | 2 +- lib/util/curry.js | 22 ++++++++++++++++++++++ lib/withDefault.js | 2 +- lib/wrap.js | 2 +- 8 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 lib/if.js diff --git a/lib/if.js b/lib/if.js new file mode 100644 index 0000000..1e9ed2d --- /dev/null +++ b/lib/if.js @@ -0,0 +1,6 @@ +import ifElse from './ifElse'; +import curry from './util/curry'; + +export default curry((predicate, trueUpdates, object) => + ifElse(predicate, trueUpdates, {}, object) +); diff --git a/lib/index.js b/lib/index.js index 77375ea..72fb5b4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,6 @@ import freeze from './freeze'; import is from './is'; +import _if from './if'; import ifElse from './ifElse'; import map from './map'; import omit from './omit'; @@ -8,11 +9,9 @@ import update from './update'; import updateIn from './updateIn'; import withDefault from './withDefault'; -import { placeholder as _ } from 'lodash/function/curry'; - const u = update; -u.if = ifElse(_, _, {}); +u.if = _if; u.ifElse = ifElse; u.is = is; u.freeze = freeze; diff --git a/lib/is.js b/lib/is.js index c5e8db0..0e3eb6d 100644 --- a/lib/is.js +++ b/lib/is.js @@ -1,5 +1,5 @@ import splitPath from './util/splitPath'; -import curry from 'lodash/function/curry'; +import curry from './util/curry'; function is(path, predicate, object) { const parts = splitPath(path); diff --git a/lib/update.js b/lib/update.js index 5640197..707b4ec 100644 --- a/lib/update.js +++ b/lib/update.js @@ -75,4 +75,4 @@ function update(updates, object, ...args) { return { ...object, ...resolvedUpdates }; } -export default wrap(update); +export default wrap(update, 2); diff --git a/lib/updateIn.js b/lib/updateIn.js index 09cc6cc..72a9d71 100644 --- a/lib/updateIn.js +++ b/lib/updateIn.js @@ -1,4 +1,4 @@ -import curry from 'lodash/function/curry'; +import curry from './util/curry'; import update from './update'; import splitPath from './util/splitPath'; diff --git a/lib/util/curry.js b/lib/util/curry.js index 1344bdb..1727308 100644 --- a/lib/util/curry.js +++ b/lib/util/curry.js @@ -28,3 +28,25 @@ export function curry3(fn) { return curried; }; } + +export function curry4(fn) { + return function curried(a, b, c, d, e, f) { + const n = arguments.length; + + if (n >= 4) return fn(a, b, c, d, e, f); + if (n === 3) return curry1((d, e, f) => fn(a, b, c, d, e, f)); + if (n === 2) return curry2((c, d, e, f) => fn(a, b, c, d, e, f)); + if (n === 1) return curry3((b, c, d, e, f) => fn(a, b, c, d, e, f)); + return curried; + }; +} + +export default function curry(fn, length = fn.length) { + return [ + fn, + curry1, + curry2, + curry3, + curry4, + ][length](fn); +} diff --git a/lib/withDefault.js b/lib/withDefault.js index 7ef9a48..3dc1fe1 100644 --- a/lib/withDefault.js +++ b/lib/withDefault.js @@ -1,5 +1,5 @@ import update from './update'; -import curry from 'lodash/function/curry'; +import curry from './util/curry'; function withDefault(defaultValue, updates, object) { if (typeof object === 'undefined') { diff --git a/lib/wrap.js b/lib/wrap.js index 1a81f4f..918e286 100644 --- a/lib/wrap.js +++ b/lib/wrap.js @@ -1,4 +1,4 @@ -import curry from 'lodash/function/curry'; +import curry from './util/curry'; import freeze from './freeze'; export default function wrap(func, length = func.length) {