Skip to content

Commit 9edec6e

Browse files
committed
Add a micro-optimization for val and var in the optimizer
1 parent 0ca987d commit 9edec6e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

async_optimizer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This is the synchronous version of the optimizer; which the Async one should be based on.
22
import { isDeterministic } from './compiler.js'
33
import { map } from './async_iterators.js'
4-
import { isSync, Sync } from './constants.js'
4+
import { isSync, Sync, OriginalImpl } from './constants.js'
55
import declareSync from './utilities/declareSync.js'
66
import { coerceArray } from './utilities/coerceArray.js'
77

@@ -29,6 +29,7 @@ function getMethod (logic, engine, methodName, above) {
2929

3030
let args = logic[methodName]
3131
if ((!args || typeof args !== 'object') && !method.optimizeUnary) args = [args]
32+
if (Array.isArray(args) && args.length === 1 && method.optimizeUnary && !Array.isArray(args[0])) args = args[0]
3233

3334
if (Array.isArray(args)) {
3435
const optimizedArgs = args.map(l => optimize(l, engine, above))
@@ -50,6 +51,11 @@ function getMethod (logic, engine, methodName, above) {
5051

5152
if (isSync(optimizedArgs) && (method.method || method[Sync])) {
5253
const called = method.method ? method.method : method
54+
if ((methodName === 'var' || methodName === 'val') && engine.methods[methodName][OriginalImpl] && ((typeof optimizedArgs === 'string' && !optimizedArgs.includes('.') && !optimizedArgs.includes('\\')) || !optimizedArgs || typeof optimizedArgs === 'number')) {
55+
if (!optimizedArgs && methodName !== 'val') return declareSync((data) => !data || typeof data === 'undefined' || (typeof data === 'function' && !engine.allowFunctions) ? null : data)
56+
return declareSync((data) => !data || typeof data[optimizedArgs] === 'undefined' || (typeof data[optimizedArgs] === 'function' && !engine.allowFunctions) ? null : data[optimizedArgs])
57+
}
58+
5359
if (method.optimizeUnary) return declareSync((data, abv) => called(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine.fallback), true)
5460
return declareSync((data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine), true)
5561
}

optimizer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// This is the synchronous version of the optimizer; which the Async one should be based on.
22
import { isDeterministic } from './compiler.js'
3+
import { OriginalImpl } from './constants.js'
34
import { coerceArray } from './utilities/coerceArray.js'
45

56
/**
@@ -21,6 +22,7 @@ function getMethod (logic, engine, methodName, above) {
2122

2223
let args = logic[methodName]
2324
if ((!args || typeof args !== 'object') && !method.optimizeUnary) args = [args]
25+
if (Array.isArray(args) && args.length === 1 && method.optimizeUnary && !Array.isArray(args[0])) args = args[0]
2426

2527
if (Array.isArray(args)) {
2628
const optimizedArgs = args.map(l => optimize(l, engine, above))
@@ -33,6 +35,10 @@ function getMethod (logic, engine, methodName, above) {
3335
const optimizedArgs = optimize(args, engine, above)
3436
if (method.optimizeUnary) {
3537
if (typeof optimizedArgs === 'function') return (data, abv) => called(optimizedArgs(data, abv), data, abv || above, engine)
38+
if ((methodName === 'var' || methodName === 'val') && engine.methods[methodName][OriginalImpl] && ((typeof optimizedArgs === 'string' && !optimizedArgs.includes('.') && !optimizedArgs.includes('\\')) || !optimizedArgs || typeof optimizedArgs === 'number')) {
39+
if (!optimizedArgs && methodName !== 'val') return (data) => !data || typeof data === 'undefined' || (typeof data === 'function' && !engine.allowFunctions) ? null : data
40+
return (data) => !data || typeof data[optimizedArgs] === 'undefined' || (typeof data[optimizedArgs] === 'function' && !engine.allowFunctions) ? null : data[optimizedArgs]
41+
}
3642
return (data, abv) => called(optimizedArgs, data, abv || above, engine)
3743
}
3844
if (typeof optimizedArgs === 'function') return (data, abv) => called(coerceArray(optimizedArgs(data, abv)), data, abv || above, engine)

0 commit comments

Comments
 (0)