Skip to content

Commit 38f86a6

Browse files
committed
Add val specific performance improvements
1 parent 4bdb739 commit 38f86a6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

optimizer.js

+20
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ function getMethod (logic, engine, methodName, above) {
8181
* Macro-type replacements to lift inefficient logic into more efficient forms.
8282
*/
8383
function checkIdioms (logic, engine, above) {
84+
// Hyper-Optimizations for val calls.
85+
if (logic.val && engine.methods.val[OriginalImpl] && Array.isArray(logic.val) && logic.val.length <= 3 && logic.val.every(i => typeof i !== 'object')) {
86+
let prev
87+
88+
if (logic.val.length === 1) {
89+
const first = logic.val[0]
90+
return (data) => (typeof (prev = (data && data[first])) !== 'function' || engine.allowFunctions) && (typeof prev !== 'undefined') ? prev : null
91+
}
92+
93+
if (logic.val.length === 2) {
94+
const [first, second] = logic.val
95+
return (data) => (typeof (prev = (data && data[first] && data[first][second])) !== 'function' || engine.allowFunctions) && (typeof prev !== 'undefined') ? prev : null
96+
}
97+
98+
if (logic.val.length === 3) {
99+
const [first, second, third] = logic.val
100+
return (data) => (typeof (prev = (data && data[first] && data[first][second] && data[first][second][third])) !== 'function' || engine.allowFunctions) && (typeof prev !== 'undefined') ? prev : null
101+
}
102+
}
103+
84104
if (logic.reduce && Array.isArray(logic.reduce)) {
85105
let [root, mapper, defaultValue] = logic.reduce
86106
if (mapper['+'] && mapper['+'].length === 2 && (mapper['+'][0] || 0).var && (mapper['+'][1] || 0).var) {

0 commit comments

Comments
 (0)