Skip to content

Commit d2b78f0

Browse files
committed
Add additional optimizations to the optimizers
1 parent 9edec6e commit d2b78f0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

async_optimizer.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function optimize (logic, engine, above = []) {
7676
engine.fallback.allowFunctions = engine.allowFunctions
7777
if (Array.isArray(logic)) {
7878
const arr = logic.map(l => optimize(l, engine, above))
79+
if (arr.every(l => typeof l !== 'function')) return arr
7980
if (isSync(arr)) return declareSync((data, abv) => arr.map(l => typeof l === 'function' ? l(data, abv) : l), true)
8081
return async (data, abv) => map(arr, l => typeof l === 'function' ? l(data, abv) : l)
8182
};

optimizer.js

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ function getMethod (logic, engine, methodName, above) {
2727
if (Array.isArray(args)) {
2828
const optimizedArgs = args.map(l => optimize(l, engine, above))
2929
if (optimizedArgs.every(l => typeof l !== 'function')) return (data, abv) => called(optimizedArgs, data, abv || above, engine)
30+
31+
if (optimizedArgs.length === 1) {
32+
const first = optimizedArgs[0]
33+
return (data, abv) => called([first(data, abv)], data, abv || above, engine)
34+
}
35+
36+
if (optimizedArgs.length === 2) {
37+
const [first, second] = optimizedArgs
38+
if (typeof first === 'function' && typeof second === 'function') return (data, abv) => called([first(data, abv), second(data, abv)], data, abv || above, engine)
39+
if (typeof first === 'function') return (data, abv) => called([first(data, abv), second], data, abv || above, engine)
40+
return (data, abv) => called([first, second(data, abv)], data, abv || above, engine)
41+
}
42+
3043
return (data, abv) => {
3144
const evaluatedArgs = optimizedArgs.map(l => typeof l === 'function' ? l(data, abv) : l)
3245
return called(evaluatedArgs, data, abv || above, engine)
@@ -41,6 +54,7 @@ function getMethod (logic, engine, methodName, above) {
4154
}
4255
return (data, abv) => called(optimizedArgs, data, abv || above, engine)
4356
}
57+
4458
if (typeof optimizedArgs === 'function') return (data, abv) => called(coerceArray(optimizedArgs(data, abv)), data, abv || above, engine)
4559
return (data, abv) => called(coerceArray(optimizedArgs), data, abv || above, engine)
4660
}
@@ -56,6 +70,7 @@ function getMethod (logic, engine, methodName, above) {
5670
export function optimize (logic, engine, above = []) {
5771
if (Array.isArray(logic)) {
5872
const arr = logic.map(l => optimize(l, engine, above))
73+
if (arr.every(l => typeof l !== 'function')) return arr
5974
return (data, abv) => arr.map(l => typeof l === 'function' ? l(data, abv) : l)
6075
};
6176

0 commit comments

Comments
 (0)