Skip to content

Commit 83f8b4d

Browse files
committed
Further improvements and optimizations (some carried over from the precision work)
1 parent ddd886e commit 83f8b4d

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

async_optimizer.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function getMethod (logic, engine, methodName, above) {
2828
}
2929

3030
let args = logic[methodName]
31-
if (!args || typeof args !== 'object') args = [args]
31+
if ((!args || typeof args !== 'object') && !method.optimizeUnary) args = [args]
3232

3333
if (Array.isArray(args)) {
3434
const optimizedArgs = args.map(l => optimize(l, engine, above))
@@ -50,12 +50,12 @@ function getMethod (logic, engine, methodName, above) {
5050

5151
if (isSync(optimizedArgs) && (method.method || method[Sync])) {
5252
const called = method.method ? method.method : method
53-
return declareSync((data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, method.optimizeUnary), data, abv || above, engine), true)
53+
if (method.optimizeUnary) return declareSync((data, abv) => called(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine.fallback), true)
54+
return declareSync((data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine), true)
5455
}
5556

56-
return async (data, abv) => {
57-
return called(coerceArray(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs, method.optimizeUnary), data, abv || above, engine)
58-
}
57+
if (method.optimizeUnary) return async (data, abv) => called(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine)
58+
return async (data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine)
5959
}
6060
}
6161

compatibility.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Sync } from './constants.js'
12
import defaultMethods from './defaultMethods.js'
23
const oldAll = defaultMethods.all
34

45
const all = {
6+
[Sync]: oldAll[Sync],
57
method: (args, context, above, engine) => {
68
if (Array.isArray(args)) {
79
const first = engine.run(args[0], context, above)

defaultMethods.js

+31-10
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ const defaultMethods = {
212212
// Why "executeInLoop"? Because if it needs to execute to get an array, I do not want to execute the arguments,
213213
// Both for performance and safety reasons.
214214
'??': {
215+
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
215216
method: (arr, _1, _2, engine) => {
216217
// See "executeInLoop" above
217218
const executeInLoop = Array.isArray(arr)
@@ -249,6 +250,7 @@ const defaultMethods = {
249250
traverse: false
250251
},
251252
or: {
253+
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
252254
method: (arr, _1, _2, engine) => {
253255
// See "executeInLoop" above
254256
const executeInLoop = Array.isArray(arr)
@@ -284,6 +286,7 @@ const defaultMethods = {
284286
traverse: false
285287
},
286288
and: {
289+
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
287290
method: (arr, _1, _2, engine) => {
288291
// See "executeInLoop" above
289292
const executeInLoop = Array.isArray(arr)
@@ -419,6 +422,7 @@ const defaultMethods = {
419422
some: createArrayIterativeMethod('some', true),
420423
all: createArrayIterativeMethod('every', true),
421424
none: {
425+
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
422426
traverse: false,
423427
// todo: add async build & build
424428
method: (val, context, above, engine) => {
@@ -439,7 +443,7 @@ const defaultMethods = {
439443
},
440444
merge: (arrays) => (Array.isArray(arrays) ? [].concat(...arrays) : [arrays]),
441445
every: createArrayIterativeMethod('every'),
442-
filter: createArrayIterativeMethod('filter'),
446+
filter: createArrayIterativeMethod('filter', true),
443447
reduce: {
444448
deterministic: (data, buildState) => {
445449
return (
@@ -548,11 +552,27 @@ const defaultMethods = {
548552
},
549553
'!': (value, _1, _2, engine) => Array.isArray(value) ? !engine.truthy(value[0]) : !engine.truthy(value),
550554
'!!': (value, _1, _2, engine) => Boolean(Array.isArray(value) ? engine.truthy(value[0]) : engine.truthy(value)),
551-
cat: (arr) => {
552-
if (typeof arr === 'string') return arr
553-
let res = ''
554-
for (let i = 0; i < arr.length; i++) res += arr[i]
555-
return res
555+
cat: {
556+
[OriginalImpl]: true,
557+
[Sync]: true,
558+
method: (arr) => {
559+
if (typeof arr === 'string') return arr
560+
if (!Array.isArray(arr)) return arr.toString()
561+
let res = ''
562+
for (let i = 0; i < arr.length; i++) res += arr[i].toString()
563+
return res
564+
},
565+
deterministic: true,
566+
traverse: true,
567+
optimizeUnary: true,
568+
compile: (data, buildState) => {
569+
if (typeof data === 'string') return JSON.stringify(data)
570+
if (typeof data === 'number') return '"' + JSON.stringify(data) + '"'
571+
if (!Array.isArray(data)) return false
572+
let res = buildState.compile`''`
573+
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} + ${data[i]}`
574+
return buildState.compile`(${res})`
575+
}
556576
},
557577
keys: ([obj]) => typeof obj === 'object' ? Object.keys(obj) : [],
558578
pipe: {
@@ -673,8 +693,8 @@ function createArrayIterativeMethod (name, useTruthy = false) {
673693
(await engine.run(selector, context, {
674694
above
675695
})) || []
676-
return asyncIterators[name](selector, (i, index) => {
677-
const result = engine.run(mapper, i, {
696+
return asyncIterators[name](selector, async (i, index) => {
697+
const result = await engine.run(mapper, i, {
678698
above: [{ iterator: selector, index }, context, above]
679699
})
680700
return useTruthy ? engine.truthy(result) : result
@@ -694,15 +714,16 @@ function createArrayIterativeMethod (name, useTruthy = false) {
694714

695715
const method = build(mapper, mapState)
696716
const aboveArray = method.aboveDetected ? buildState.compile`[{ iterator: z, index: x }, context, above]` : buildState.compile`null`
717+
const useTruthyMethod = useTruthy ? buildState.compile`engine.truthy` : buildState.compile``
697718

698719
if (async) {
699720
if (!isSync(method)) {
700721
buildState.asyncDetected = true
701-
return buildState.compile`await asyncIterators[${name}](${selector} || [], async (i, x, z) => ${method}(i, x, ${aboveArray}))`
722+
return buildState.compile`await asyncIterators[${name}](${selector} || [], async (i, x, z) => ${useTruthyMethod}(${method}(i, x, ${aboveArray})))`
702723
}
703724
}
704725

705-
return buildState.compile`(${selector} || [])[${name}]((i, x, z) => ${method}(i, x, ${aboveArray}))`
726+
return buildState.compile`(${selector} || [])[${name}]((i, x, z) => ${useTruthyMethod}(${method}(i, x, ${aboveArray})))`
706727
},
707728
traverse: false
708729
}

optimizer.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getMethod (logic, engine, methodName, above) {
2020
}
2121

2222
let args = logic[methodName]
23-
if (!args || typeof args !== 'object') args = [args]
23+
if ((!args || typeof args !== 'object') && !method.optimizeUnary) args = [args]
2424

2525
if (Array.isArray(args)) {
2626
const optimizedArgs = args.map(l => optimize(l, engine, above))
@@ -30,9 +30,8 @@ function getMethod (logic, engine, methodName, above) {
3030
}
3131
} else {
3232
const optimizedArgs = optimize(args, engine, above)
33-
return (data, abv) => {
34-
return called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, method.optimizeUnary), data, abv || above, engine)
35-
}
33+
if (method.optimizeUnary) return (data, abv) => called(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine)
34+
return (data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine)
3635
}
3736
}
3837

0 commit comments

Comments
 (0)