rubico 1.9.7 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +5 -0
- package/.eslintrc.js +3 -2
- package/.github/workflows/nodejs.yml +1 -1
- package/AggregateReducer.js +19 -0
- package/AggregateReducer.test.js +82 -0
- package/CHANGELOG.md +22 -1
- package/README.md +158 -81
- package/Transducer.js +75 -26
- package/Transducer.test.js +117 -0
- package/_internal/ComparisonOperator.js +68 -0
- package/_internal/File.js +41 -0
- package/_internal/FlatMappingAsyncIterator.js +4 -4
- package/_internal/FlatMappingIterator.js +1 -1
- package/_internal/TimeInLoopSuite.js +138 -0
- package/_internal/areAnyValuesPromises.js +23 -0
- package/_internal/{arrayAll.js → arrayEvery.js} +4 -4
- package/_internal/arrayExtend.js +3 -2
- package/_internal/arrayFilter.js +3 -4
- package/_internal/{arrayAny.js → arraySome.js} +8 -8
- package/_internal/{asyncIteratorAll.js → asyncIteratorEvery.js} +4 -4
- package/_internal/{asyncIteratorAny.js → asyncIteratorSome.js} +4 -4
- package/_internal/curry4.test.js +25 -0
- package/_internal/curry5.test.js +29 -0
- package/_internal/curryArgs2.js +43 -0
- package/_internal/curryArgs3.test.js +21 -0
- package/_internal/curryArgs4.js +65 -0
- package/_internal/curryArgs4.test.js +25 -0
- package/_internal/equals.js +13 -0
- package/_internal/findAllFilePaths.js +22 -0
- package/_internal/funcApply.js +2 -0
- package/_internal/funcCall.js +14 -0
- package/_internal/functionArrayAll.js +26 -0
- package/_internal/functionArrayAllSeries.js +55 -0
- package/_internal/functionObjectAll.js +32 -0
- package/_internal/genericReduce.js +4 -22
- package/_internal/genericTransform.js +15 -15
- package/_internal/improvedGenericTransform.js +93 -0
- package/_internal/{iteratorAll.js → iteratorEvery.js} +4 -4
- package/_internal/{iteratorAny.js → iteratorSome.js} +6 -6
- package/_internal/leftResolverRightResolverCompare.js +19 -0
- package/_internal/leftResolverRightValueCompare.js +16 -0
- package/_internal/leftValueRightResolverCompare.js +16 -0
- package/_internal/objectReduce.js +0 -1
- package/_internal/pathResolve.js +6 -0
- package/_internal/{reducerAll.js → reducerEvery.js} +4 -4
- package/_internal/reducerFlatMap.js +4 -4
- package/_internal/reducerFlatten.js +1 -1
- package/_internal/{reducerAny.js → reducerSome.js} +4 -4
- package/_internal/timeInLoop.js +37 -0
- package/_internal/timeInLoop.test.js +18 -0
- package/_internal/timeInLoopAsync.js +35 -0
- package/_internal/timeInLoopAsync.test.js +22 -0
- package/all.js +106 -40
- package/and.js +54 -36
- package/archive/{FlatMappingIteratorCool.js → FlatMappingIterator-2020-09-28.js} +1 -1
- package/archive/_Promise-2023-05-29.js +93 -0
- package/archive/arrayMap2-2023-05-29.js +73 -0
- package/archive/benchmarks-v1.9.7/all.js +34 -0
- package/archive/benchmarks-v1.9.7/and.js +24 -0
- package/archive/benchmarks-v1.9.7/assign.js +174 -0
- package/archive/benchmarks-v1.9.7/curry.js +55 -0
- package/archive/benchmarks-v1.9.7/eq.js +25 -0
- package/archive/benchmarks-v1.9.7/filter.js +1322 -0
- package/archive/benchmarks-v1.9.7/flatMap.js +48 -0
- package/archive/benchmarks-v1.9.7/get.js +44 -0
- package/archive/benchmarks-v1.9.7/gt.js +25 -0
- package/archive/benchmarks-v1.9.7/gte.js +25 -0
- package/archive/benchmarks-v1.9.7/lt.js +25 -0
- package/archive/benchmarks-v1.9.7/lte.js +25 -0
- package/archive/benchmarks-v1.9.7/map.js +892 -0
- package/archive/benchmarks-v1.9.7/omit.js +28 -0
- package/archive/benchmarks-v1.9.7/or.js +51 -0
- package/archive/benchmarks-v1.9.7/pick.js +24 -0
- package/archive/benchmarks-v1.9.7/pipe.js +152 -0
- package/archive/benchmarks-v1.9.7/reduce.js +739 -0
- package/archive/benchmarks-v1.9.7/switchCase.js +256 -0
- package/archive/benchmarks-v1.9.7/tap.js +90 -0
- package/archive/benchmarks-v1.9.7/transform.js +218 -0
- package/archive/benchmarks-v1.9.7/tryCatch.js +108 -0
- package/assign.js +18 -8
- package/bench +65 -0
- package/benchmark-output/v1.9.7 +268 -0
- package/benchmarks/all.async.js +43 -0
- package/benchmarks/all.js +42 -33
- package/benchmarks/always.js +15 -0
- package/benchmarks/and.async.js +25 -0
- package/benchmarks/and.js +20 -19
- package/benchmarks/assign.async.js +27 -0
- package/benchmarks/assign.js +20 -167
- package/benchmarks/curry.js +34 -54
- package/benchmarks/eq.async.js +23 -0
- package/benchmarks/eq.js +17 -19
- package/benchmarks/every.async.js +19 -0
- package/benchmarks/every.js +19 -0
- package/benchmarks/filter.async.js +32 -0
- package/benchmarks/filter.js +27 -1311
- package/benchmarks/flatMap.async.js +26 -0
- package/benchmarks/flatMap.js +26 -36
- package/benchmarks/get.async.js +19 -0
- package/benchmarks/get.js +27 -32
- package/benchmarks/gt.async.js +23 -0
- package/benchmarks/gt.js +17 -19
- package/benchmarks/gte.async.js +23 -0
- package/benchmarks/gte.js +17 -19
- package/benchmarks/lt.async.js +23 -0
- package/benchmarks/lt.js +17 -19
- package/benchmarks/lte.async.js +23 -0
- package/benchmarks/lte.js +17 -19
- package/benchmarks/map.async.js +43 -0
- package/benchmarks/map.js +24 -876
- package/benchmarks/misc/Promise.js +26 -0
- package/benchmarks/misc/isPromise.js +30 -0
- package/benchmarks/misc/promiseAll.js +36 -0
- package/benchmarks/not.js +23 -0
- package/benchmarks/omit.js +30 -20
- package/benchmarks/or.async.js +25 -0
- package/benchmarks/or.js +23 -49
- package/benchmarks/pick.js +30 -16
- package/benchmarks/pipe.async.js +47 -0
- package/benchmarks/pipe.js +46 -151
- package/benchmarks/reduce.async.js +32 -0
- package/benchmarks/reduce.js +27 -728
- package/benchmarks/set.async.js +19 -0
- package/benchmarks/set.js +41 -0
- package/benchmarks/some.async.js +19 -0
- package/benchmarks/some.js +19 -0
- package/benchmarks/switchCase.async.js +27 -0
- package/benchmarks/switchCase.js +55 -256
- package/benchmarks/tap.js +10 -85
- package/benchmarks/thunkify.js +15 -0
- package/benchmarks/transform.async.js +27 -0
- package/benchmarks/transform.js +28 -206
- package/benchmarks/tryCatch.async.js +25 -0
- package/benchmarks/tryCatch.js +24 -100
- package/build +472 -0
- package/compose.js +46 -0
- package/dist/Transducer.es.js +481 -0
- package/dist/Transducer.es.min.js +7 -0
- package/dist/Transducer.js +488 -0
- package/dist/Transducer.min.js +8 -0
- package/dist/Transducer.mjs +481 -0
- package/dist/__.es.js +2 -2
- package/dist/__.es.min.js +2 -2
- package/dist/__.js +2 -2
- package/dist/__.min.js +2 -2
- package/dist/__.mjs +2 -2
- package/dist/all.es.js +173 -92
- package/dist/all.es.min.js +3 -3
- package/dist/all.js +173 -92
- package/dist/all.min.js +3 -3
- package/dist/all.mjs +173 -92
- package/dist/always.es.js +2 -2
- package/dist/always.es.min.js +2 -2
- package/dist/always.js +2 -2
- package/dist/always.min.js +2 -2
- package/dist/always.mjs +2 -2
- package/dist/and.es.js +101 -32
- package/dist/and.es.min.js +3 -3
- package/dist/and.js +101 -32
- package/dist/and.min.js +3 -3
- package/dist/and.mjs +101 -32
- package/dist/assign.es.js +37 -10
- package/dist/assign.es.min.js +3 -3
- package/dist/assign.js +37 -10
- package/dist/assign.min.js +3 -3
- package/dist/assign.mjs +37 -10
- package/dist/compose.es.js +71 -0
- package/dist/compose.es.min.js +7 -0
- package/dist/compose.js +78 -0
- package/dist/compose.min.js +8 -0
- package/dist/compose.mjs +71 -0
- package/dist/curry.es.js +2 -2
- package/dist/curry.es.min.js +2 -2
- package/dist/curry.js +2 -2
- package/dist/curry.min.js +2 -2
- package/dist/curry.mjs +2 -2
- package/dist/eq.es.js +172 -49
- package/dist/eq.es.min.js +3 -3
- package/dist/eq.js +172 -49
- package/dist/eq.min.js +3 -3
- package/dist/eq.mjs +172 -49
- package/dist/every.es.js +194 -0
- package/dist/every.es.min.js +7 -0
- package/dist/every.js +201 -0
- package/dist/every.min.js +8 -0
- package/dist/every.mjs +194 -0
- package/dist/filter.es.js +14 -108
- package/dist/filter.es.min.js +3 -3
- package/dist/filter.js +14 -108
- package/dist/filter.min.js +3 -3
- package/dist/filter.mjs +14 -108
- package/dist/flatMap.es.js +27 -214
- package/dist/flatMap.es.min.js +3 -3
- package/dist/flatMap.js +27 -214
- package/dist/flatMap.min.js +3 -3
- package/dist/flatMap.mjs +27 -214
- package/dist/{x/forEach.es.js → forEach.es.js} +52 -70
- package/dist/forEach.es.min.js +7 -0
- package/dist/{x/forEach.js → forEach.js} +52 -70
- package/dist/forEach.min.js +8 -0
- package/dist/{x/forEach.mjs → forEach.mjs} +52 -70
- package/dist/get.es.js +60 -5
- package/dist/get.es.min.js +3 -3
- package/dist/get.js +60 -5
- package/dist/get.min.js +3 -3
- package/dist/get.mjs +60 -5
- package/dist/gt.es.js +171 -48
- package/dist/gt.es.min.js +3 -3
- package/dist/gt.js +171 -48
- package/dist/gt.min.js +3 -3
- package/dist/gt.mjs +171 -48
- package/dist/gte.es.js +171 -48
- package/dist/gte.es.min.js +3 -3
- package/dist/gte.js +171 -48
- package/dist/gte.min.js +3 -3
- package/dist/gte.mjs +171 -48
- package/dist/lt.es.js +171 -48
- package/dist/lt.es.min.js +3 -3
- package/dist/lt.js +171 -48
- package/dist/lt.min.js +3 -3
- package/dist/lt.mjs +171 -48
- package/dist/lte.es.js +171 -48
- package/dist/lte.es.min.js +3 -3
- package/dist/lte.js +171 -48
- package/dist/lte.min.js +3 -3
- package/dist/lte.mjs +171 -48
- package/dist/map.es.js +12 -101
- package/dist/map.es.min.js +3 -3
- package/dist/map.js +12 -101
- package/dist/map.min.js +3 -3
- package/dist/map.mjs +12 -101
- package/dist/not.es.js +75 -14
- package/dist/not.es.min.js +3 -3
- package/dist/not.js +75 -14
- package/dist/not.min.js +3 -3
- package/dist/not.mjs +75 -14
- package/dist/omit.es.js +7 -2
- package/dist/omit.es.min.js +3 -3
- package/dist/omit.js +7 -2
- package/dist/omit.min.js +3 -3
- package/dist/omit.mjs +7 -2
- package/dist/or.es.js +99 -31
- package/dist/or.es.min.js +3 -3
- package/dist/or.js +99 -31
- package/dist/or.min.js +3 -3
- package/dist/or.mjs +99 -31
- package/dist/pick.es.js +8 -3
- package/dist/pick.es.min.js +3 -3
- package/dist/pick.js +8 -3
- package/dist/pick.min.js +3 -3
- package/dist/pick.mjs +8 -3
- package/dist/pipe.es.js +42 -46
- package/dist/pipe.es.min.js +3 -3
- package/dist/pipe.js +42 -46
- package/dist/pipe.min.js +3 -3
- package/dist/pipe.mjs +42 -46
- package/dist/reduce.es.js +52 -94
- package/dist/reduce.es.min.js +3 -3
- package/dist/reduce.js +52 -94
- package/dist/reduce.min.js +3 -3
- package/dist/reduce.mjs +52 -94
- package/dist/rubico.es.js +762 -883
- package/dist/rubico.es.min.js +3 -3
- package/dist/rubico.global.js +2794 -0
- package/dist/rubico.global.min.js +8 -0
- package/dist/rubico.js +739 -869
- package/dist/rubico.min.js +3 -3
- package/dist/rubico.mjs +762 -883
- package/dist/set.es.js +18 -3
- package/dist/set.es.min.js +3 -3
- package/dist/set.js +18 -3
- package/dist/set.min.js +3 -3
- package/dist/set.mjs +18 -3
- package/dist/{any.js → some.es.js} +64 -54
- package/dist/some.es.min.js +7 -0
- package/dist/{any.mjs → some.js} +72 -48
- package/dist/some.min.js +8 -0
- package/dist/{any.es.js → some.mjs} +65 -48
- package/dist/switchCase.es.js +55 -5
- package/dist/switchCase.es.min.js +3 -3
- package/dist/switchCase.js +55 -5
- package/dist/switchCase.min.js +3 -3
- package/dist/switchCase.mjs +55 -5
- package/dist/tap.es.js +2 -9
- package/dist/tap.es.min.js +3 -3
- package/dist/tap.js +2 -9
- package/dist/tap.min.js +3 -3
- package/dist/tap.mjs +2 -9
- package/dist/thunkify.es.js +45 -2
- package/dist/thunkify.es.min.js +3 -3
- package/dist/thunkify.js +45 -2
- package/dist/thunkify.min.js +3 -3
- package/dist/thunkify.mjs +45 -2
- package/dist/transform.es.js +35 -71
- package/dist/transform.es.min.js +3 -3
- package/dist/transform.js +35 -71
- package/dist/transform.min.js +3 -3
- package/dist/transform.mjs +35 -71
- package/dist/tryCatch.es.js +33 -17
- package/dist/tryCatch.es.min.js +3 -3
- package/dist/tryCatch.js +33 -17
- package/dist/tryCatch.min.js +3 -3
- package/dist/tryCatch.mjs +33 -17
- package/dist/x/append.es.js +2 -2
- package/dist/x/append.es.min.js +2 -2
- package/dist/x/append.js +2 -2
- package/dist/x/append.min.js +2 -2
- package/dist/x/append.mjs +2 -2
- package/dist/x/callProp.es.js +2 -2
- package/dist/x/callProp.es.min.js +2 -2
- package/dist/x/callProp.js +2 -2
- package/dist/x/callProp.min.js +2 -2
- package/dist/x/callProp.mjs +2 -2
- package/dist/x/defaultsDeep.es.js +2 -2
- package/dist/x/defaultsDeep.es.min.js +2 -2
- package/dist/x/defaultsDeep.js +2 -2
- package/dist/x/defaultsDeep.min.js +2 -2
- package/dist/x/defaultsDeep.mjs +2 -2
- package/dist/x/differenceWith.es.js +7 -7
- package/dist/x/differenceWith.es.min.js +3 -3
- package/dist/x/differenceWith.js +7 -7
- package/dist/x/differenceWith.min.js +2 -2
- package/dist/x/differenceWith.mjs +7 -7
- package/dist/x/filterOut.es.js +60 -117
- package/dist/x/filterOut.es.min.js +3 -3
- package/dist/x/filterOut.js +60 -117
- package/dist/x/filterOut.min.js +3 -3
- package/dist/x/filterOut.mjs +60 -117
- package/dist/x/find.es.js +2 -2
- package/dist/x/find.es.min.js +2 -2
- package/dist/x/find.js +2 -2
- package/dist/x/find.min.js +2 -2
- package/dist/x/find.mjs +2 -2
- package/dist/x/findIndex.es.js +2 -2
- package/dist/x/findIndex.es.min.js +2 -2
- package/dist/x/findIndex.js +2 -2
- package/dist/x/findIndex.min.js +2 -2
- package/dist/x/findIndex.mjs +2 -2
- package/dist/x/first.es.js +2 -2
- package/dist/x/first.es.min.js +2 -2
- package/dist/x/first.js +2 -2
- package/dist/x/first.min.js +2 -2
- package/dist/x/first.mjs +2 -2
- package/dist/x/flatten.es.js +20 -207
- package/dist/x/flatten.es.min.js +3 -3
- package/dist/x/flatten.js +20 -207
- package/dist/x/flatten.min.js +3 -3
- package/dist/x/flatten.mjs +20 -207
- package/dist/x/groupBy.es.js +52 -94
- package/dist/x/groupBy.es.min.js +3 -3
- package/dist/x/groupBy.js +52 -94
- package/dist/x/groupBy.min.js +3 -3
- package/dist/x/groupBy.mjs +52 -94
- package/dist/x/has.es.js +2 -2
- package/dist/x/has.es.min.js +2 -2
- package/dist/x/has.js +2 -2
- package/dist/x/has.min.js +2 -2
- package/dist/x/has.mjs +2 -2
- package/dist/x/identity.es.js +2 -2
- package/dist/x/identity.es.min.js +2 -2
- package/dist/x/identity.js +2 -2
- package/dist/x/identity.min.js +2 -2
- package/dist/x/identity.mjs +2 -2
- package/dist/x/includes.es.js +2 -2
- package/dist/x/includes.es.min.js +2 -2
- package/dist/x/includes.js +2 -2
- package/dist/x/includes.min.js +2 -2
- package/dist/x/includes.mjs +2 -2
- package/dist/x/isDeepEqual.es.js +2 -2
- package/dist/x/isDeepEqual.es.min.js +2 -2
- package/dist/x/isDeepEqual.js +2 -2
- package/dist/x/isDeepEqual.min.js +2 -2
- package/dist/x/isDeepEqual.mjs +2 -2
- package/dist/x/isEmpty.es.js +2 -2
- package/dist/x/isEmpty.es.min.js +2 -2
- package/dist/x/isEmpty.js +2 -2
- package/dist/x/isEmpty.min.js +2 -2
- package/dist/x/isEmpty.mjs +2 -2
- package/dist/x/isEqual.es.js +2 -2
- package/dist/x/isEqual.es.min.js +2 -2
- package/dist/x/isEqual.js +2 -2
- package/dist/x/isEqual.min.js +2 -2
- package/dist/x/isEqual.mjs +2 -2
- package/dist/x/isFunction.es.js +2 -2
- package/dist/x/isFunction.es.min.js +2 -2
- package/dist/x/isFunction.js +2 -2
- package/dist/x/isFunction.min.js +2 -2
- package/dist/x/isFunction.mjs +2 -2
- package/dist/x/isIn.es.js +2 -2
- package/dist/x/isIn.es.min.js +2 -2
- package/dist/x/isIn.js +2 -2
- package/dist/x/isIn.min.js +2 -2
- package/dist/x/isIn.mjs +2 -2
- package/dist/x/isObject.es.js +2 -2
- package/dist/x/isObject.es.min.js +2 -2
- package/dist/x/isObject.js +2 -2
- package/dist/x/isObject.min.js +2 -2
- package/dist/x/isObject.mjs +2 -2
- package/dist/x/isString.es.js +2 -2
- package/dist/x/isString.es.min.js +2 -2
- package/dist/x/isString.js +2 -2
- package/dist/x/isString.min.js +2 -2
- package/dist/x/isString.mjs +2 -2
- package/dist/x/keys.es.js +2 -2
- package/dist/x/keys.es.min.js +2 -2
- package/dist/x/keys.js +2 -2
- package/dist/x/keys.min.js +2 -2
- package/dist/x/keys.mjs +2 -2
- package/dist/x/last.es.js +2 -2
- package/dist/x/last.es.min.js +2 -2
- package/dist/x/last.js +2 -2
- package/dist/x/last.min.js +2 -2
- package/dist/x/last.mjs +2 -2
- package/dist/x/maxBy.es.js +59 -6
- package/dist/x/maxBy.es.min.js +3 -3
- package/dist/x/maxBy.js +59 -6
- package/dist/x/maxBy.min.js +3 -3
- package/dist/x/maxBy.mjs +59 -6
- package/dist/x/noop.es.js +2 -2
- package/dist/x/noop.es.min.js +2 -2
- package/dist/x/noop.js +2 -2
- package/dist/x/noop.min.js +2 -2
- package/dist/x/noop.mjs +2 -2
- package/dist/x/pluck.es.js +32 -103
- package/dist/x/pluck.es.min.js +3 -3
- package/dist/x/pluck.js +32 -103
- package/dist/x/pluck.min.js +3 -3
- package/dist/x/pluck.mjs +32 -103
- package/dist/x/prepend.es.js +2 -2
- package/dist/x/prepend.es.min.js +2 -2
- package/dist/x/prepend.js +2 -2
- package/dist/x/prepend.min.js +2 -2
- package/dist/x/prepend.mjs +2 -2
- package/dist/x/size.es.js +2 -2
- package/dist/x/size.es.min.js +2 -2
- package/dist/x/size.js +2 -2
- package/dist/x/size.min.js +2 -2
- package/dist/x/size.mjs +2 -2
- package/dist/x/trace.es.js +2 -9
- package/dist/x/trace.es.min.js +3 -3
- package/dist/x/trace.js +2 -9
- package/dist/x/trace.min.js +3 -3
- package/dist/x/trace.mjs +2 -9
- package/dist/x/unionWith.es.js +2 -2
- package/dist/x/unionWith.es.min.js +2 -2
- package/dist/x/unionWith.js +2 -2
- package/dist/x/unionWith.min.js +2 -2
- package/dist/x/unionWith.mjs +2 -2
- package/dist/x/uniq.es.js +2 -2
- package/dist/x/uniq.es.min.js +2 -2
- package/dist/x/uniq.js +2 -2
- package/dist/x/uniq.min.js +2 -2
- package/dist/x/uniq.mjs +2 -2
- package/dist/x/unless.es.js +2 -2
- package/dist/x/unless.es.min.js +2 -2
- package/dist/x/unless.js +2 -2
- package/dist/x/unless.min.js +2 -2
- package/dist/x/unless.mjs +2 -2
- package/dist/x/values.es.js +2 -2
- package/dist/x/values.es.min.js +2 -2
- package/dist/x/values.js +2 -2
- package/dist/x/values.min.js +2 -2
- package/dist/x/values.mjs +2 -2
- package/dist/x/when.es.js +2 -2
- package/dist/x/when.es.min.js +2 -2
- package/dist/x/when.js +2 -2
- package/dist/x/when.min.js +2 -2
- package/dist/x/when.mjs +2 -2
- package/dist-test.js +31 -2042
- package/eq.js +21 -63
- package/es.js +739 -869
- package/every.js +94 -0
- package/filter.js +23 -91
- package/flatMap.js +75 -70
- package/forEach.js +82 -0
- package/get.js +23 -6
- package/global.js +6 -4
- package/gt.js +15 -58
- package/gte.js +14 -57
- package/index.js +739 -869
- package/lt.js +17 -60
- package/lte.js +14 -57
- package/map.js +9 -94
- package/memory-usage/reduce.js +2 -2
- package/not.js +27 -45
- package/omit.js +4 -0
- package/or.js +50 -33
- package/package.json +7 -7
- package/pick.js +5 -1
- package/pipe.js +13 -50
- package/reduce.js +42 -51
- package/rubico.js +22 -11
- package/set.js +43 -10
- package/some.js +105 -0
- package/switchCase.js +13 -3
- package/tap.js +0 -24
- package/test.js +847 -1096
- package/thunkify.js +9 -0
- package/tmp.js +1 -0
- package/transform.js +31 -16
- package/tryCatch.js +19 -15
- package/x/differenceWith.js +3 -3
- package/x/filterOut.test.js +6 -6
- package/x/flatten.js +1 -1
- package/x/flatten.test.js +0 -8
- package/x/index.js +0 -2
- package/x/pluck.js +18 -27
- package/x/pluck.test.js +3 -7
- package/x/timeInLoop.js +16 -25
- package/x/timeInLoop.test.js +8 -4
- package/any.js +0 -82
- package/dist/__.min.mjs +0 -7
- package/dist/all.min.mjs +0 -7
- package/dist/always.min.mjs +0 -7
- package/dist/and.min.mjs +0 -7
- package/dist/any.es.min.js +0 -7
- package/dist/any.min.js +0 -8
- package/dist/any.min.mjs +0 -7
- package/dist/assign.min.mjs +0 -7
- package/dist/curry.min.mjs +0 -7
- package/dist/eq.min.mjs +0 -7
- package/dist/filter.min.mjs +0 -7
- package/dist/flatMap.min.mjs +0 -7
- package/dist/fork.es.js +0 -159
- package/dist/fork.es.min.js +0 -7
- package/dist/fork.js +0 -166
- package/dist/fork.min.js +0 -8
- package/dist/fork.min.mjs +0 -7
- package/dist/fork.mjs +0 -159
- package/dist/get.min.mjs +0 -7
- package/dist/gt.min.mjs +0 -7
- package/dist/gte.min.mjs +0 -7
- package/dist/lt.min.mjs +0 -7
- package/dist/lte.min.mjs +0 -7
- package/dist/map.min.mjs +0 -7
- package/dist/not.min.mjs +0 -7
- package/dist/omit.min.mjs +0 -7
- package/dist/or.min.mjs +0 -7
- package/dist/pick.min.mjs +0 -7
- package/dist/pipe.min.mjs +0 -7
- package/dist/reduce.min.mjs +0 -7
- package/dist/rubico.min.mjs +0 -7
- package/dist/set.min.mjs +0 -7
- package/dist/switchCase.min.mjs +0 -7
- package/dist/tap.min.mjs +0 -7
- package/dist/thunkify.min.mjs +0 -7
- package/dist/transform.min.mjs +0 -7
- package/dist/tryCatch.min.mjs +0 -7
- package/dist/x/append.min.mjs +0 -7
- package/dist/x/callProp.min.mjs +0 -7
- package/dist/x/defaultsDeep.min.mjs +0 -7
- package/dist/x/differenceWith.min.mjs +0 -7
- package/dist/x/filterOut.min.mjs +0 -7
- package/dist/x/find.min.mjs +0 -7
- package/dist/x/findIndex.min.mjs +0 -7
- package/dist/x/first.min.mjs +0 -7
- package/dist/x/flatten.min.mjs +0 -7
- package/dist/x/forEach.es.min.js +0 -7
- package/dist/x/forEach.min.js +0 -8
- package/dist/x/forEach.min.mjs +0 -7
- package/dist/x/groupBy.min.mjs +0 -7
- package/dist/x/has.min.mjs +0 -7
- package/dist/x/identity.min.mjs +0 -7
- package/dist/x/includes.min.mjs +0 -7
- package/dist/x/isDeepEqual.min.mjs +0 -7
- package/dist/x/isEmpty.min.mjs +0 -7
- package/dist/x/isEqual.min.mjs +0 -7
- package/dist/x/isFunction.min.mjs +0 -7
- package/dist/x/isIn.min.mjs +0 -7
- package/dist/x/isObject.min.mjs +0 -7
- package/dist/x/isString.min.mjs +0 -7
- package/dist/x/keys.min.mjs +0 -7
- package/dist/x/last.min.mjs +0 -7
- package/dist/x/maxBy.min.mjs +0 -7
- package/dist/x/noop.min.mjs +0 -7
- package/dist/x/pluck.min.mjs +0 -7
- package/dist/x/prepend.min.mjs +0 -7
- package/dist/x/size.min.mjs +0 -7
- package/dist/x/trace.min.mjs +0 -7
- package/dist/x/unionWith.min.mjs +0 -7
- package/dist/x/uniq.min.mjs +0 -7
- package/dist/x/unless.min.mjs +0 -7
- package/dist/x/values.min.mjs +0 -7
- package/dist/x/when.min.mjs +0 -7
- package/distributor.js +0 -495
- package/fork.js +0 -101
- package/x/forEach.js +0 -93
- package/x/forEach.test.js +0 -218
- /package/{benchmarks → archive/benchmarks-v1.9.7}/any.js +0 -0
- /package/{x/forEach.benchmark.js → archive/benchmarks-v1.9.7/forEach.js} +0 -0
- /package/{benchmarks → archive/benchmarks-v1.9.7}/fork.js +0 -0
- /package/{benchmarks → archive/benchmarks-v1.9.7}/integration.js +0 -0
- /package/{benchmarks → archive/benchmarks-v1.9.7}/misc.js +0 -0
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
const timeInLoop = require('../x/timeInLoop')
|
|
2
|
+
const { map } = require('..')
|
|
3
|
+
const R = require('ramda')
|
|
4
|
+
const _ = require('lodash')
|
|
5
|
+
|
|
6
|
+
const symbolIterator = Symbol.iterator
|
|
7
|
+
|
|
8
|
+
const symbolAsyncIterator = Symbol.asyncIterator
|
|
9
|
+
|
|
10
|
+
const objectKeys = Object.keys
|
|
11
|
+
|
|
12
|
+
const promiseAll = Promise.all.bind(Promise)
|
|
13
|
+
|
|
14
|
+
const promiseRace = Promise.race.bind(Promise)
|
|
15
|
+
|
|
16
|
+
const isPromise = value => value != null && typeof value.then == 'function'
|
|
17
|
+
|
|
18
|
+
const asyncIteratorToArray = async asyncIter => {
|
|
19
|
+
const result = []
|
|
20
|
+
for await (const item of asyncIter) result.push(item)
|
|
21
|
+
return result
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const arrayMap1 = function (array, func) {
|
|
26
|
+
let isAsync = false
|
|
27
|
+
const output = array.map(function arrayMapper(item) {
|
|
28
|
+
const outputItem = func(item)
|
|
29
|
+
if (isPromise(outputItem)) isAsync = true
|
|
30
|
+
return item
|
|
31
|
+
})
|
|
32
|
+
return isAsync ? promiseAll(output) : output
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const arrayMap2 = function (array, func) {
|
|
36
|
+
const arrayLength = array.length, output = []
|
|
37
|
+
let index = -1, isAsync = false
|
|
38
|
+
while (++index < arrayLength) {
|
|
39
|
+
const outputItem = func(array[index])
|
|
40
|
+
if (isPromise(outputItem)) isAsync = true
|
|
41
|
+
output[index] = outputItem
|
|
42
|
+
}
|
|
43
|
+
return isAsync ? promiseAll(output) : output
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const arrayMap20 = function (array, func) {
|
|
47
|
+
const arrayLength = array.length, output = []
|
|
48
|
+
let index = -1, isAsync = false
|
|
49
|
+
while (++index < arrayLength) {
|
|
50
|
+
const outputItem = func(array[index])
|
|
51
|
+
if (isPromise(outputItem)) isAsync = true
|
|
52
|
+
output.push(outputItem)
|
|
53
|
+
}
|
|
54
|
+
return isAsync ? promiseAll(output) : output
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const arrayMap3 = function (array, func) {
|
|
58
|
+
const arrayLength = array.length,
|
|
59
|
+
output = Array(arrayLength)
|
|
60
|
+
let index = -1, isAsync = false
|
|
61
|
+
while (++index < arrayLength) {
|
|
62
|
+
const outputItem = func(array[index])
|
|
63
|
+
if (isPromise(outputItem)) isAsync = true
|
|
64
|
+
output[index] = outputItem
|
|
65
|
+
}
|
|
66
|
+
return isAsync ? promiseAll(output) : output
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @name arrayMap
|
|
71
|
+
*
|
|
72
|
+
* @benchmark
|
|
73
|
+
* vanilla1: 1e+6: 44.19ms
|
|
74
|
+
* arrayMap1: 1e+6: 46.194ms
|
|
75
|
+
* arrayMap2: 1e+6: 65.156ms
|
|
76
|
+
* arrayMap20: 1e+6: 62.963ms
|
|
77
|
+
* arrayMap3: 1e+6: 29.981ms
|
|
78
|
+
*
|
|
79
|
+
* arrayMap1_forOf: 1e+6: 19.514ms
|
|
80
|
+
* arrayMap2_forOf: 1e+6: 19.406ms
|
|
81
|
+
* arrayMap3_forOf: 1e+6: 20.483ms
|
|
82
|
+
*
|
|
83
|
+
* arrayMap1_forLoop: 1e+6: 9.992ms
|
|
84
|
+
* arrayMap2_forLoop: 1e+6: 9.958ms
|
|
85
|
+
* arrayMap3_forLoop: 1e+6: 9.929ms
|
|
86
|
+
*
|
|
87
|
+
* RMap: 1e+6: 217.641ms
|
|
88
|
+
* _Map: 1e+6: 32.702ms
|
|
89
|
+
*
|
|
90
|
+
* @NOTE Bo5
|
|
91
|
+
*
|
|
92
|
+
* @NOTE https://reinteractive.net/posts/292-the-quirky-array-constructor-and-a-use-for-holey-arrays-in-es6
|
|
93
|
+
*
|
|
94
|
+
* @NOTE https://v8.dev/blog/elements-kinds#avoid-creating-holes
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
{
|
|
98
|
+
const array = [1, 2, 3, 4, 5]
|
|
99
|
+
|
|
100
|
+
const identity = value => value
|
|
101
|
+
|
|
102
|
+
const array1 = arrayMap1(array, identity)
|
|
103
|
+
|
|
104
|
+
const array2 = arrayMap2(array, identity)
|
|
105
|
+
|
|
106
|
+
const array3 = arrayMap3(array, identity)
|
|
107
|
+
|
|
108
|
+
const vanilla1 = (arr, func) => arr.map(func)
|
|
109
|
+
|
|
110
|
+
const RMap = R.map
|
|
111
|
+
|
|
112
|
+
const _Map = _.map
|
|
113
|
+
|
|
114
|
+
// timeInLoop('vanilla1', 1e6, () => vanilla1(array, identity))
|
|
115
|
+
|
|
116
|
+
// timeInLoop('arrayMap1', 1e6, () => arrayMap1(array, identity))
|
|
117
|
+
|
|
118
|
+
// timeInLoop('arrayMap2', 1e6, () => arrayMap2(array, identity))
|
|
119
|
+
|
|
120
|
+
// timeInLoop('arrayMap20', 1e6, () => arrayMap20(array, identity))
|
|
121
|
+
|
|
122
|
+
// timeInLoop('arrayMap3', 1e6, () => arrayMap3(array, identity))
|
|
123
|
+
|
|
124
|
+
// timeInLoop('arrayMap4', 1e6, () => arrayMap4(array, identity))
|
|
125
|
+
|
|
126
|
+
// timeInLoop('arrayMap1_forOf', 1e6, () => { for (const item of array1) identity(item) })
|
|
127
|
+
|
|
128
|
+
// timeInLoop('arrayMap2_forOf', 1e6, () => { for (const item of array2) identity(item) })
|
|
129
|
+
|
|
130
|
+
// timeInLoop('arrayMap3_forOf', 1e6, () => { for (const item of array3) identity(item) })
|
|
131
|
+
|
|
132
|
+
// timeInLoop('arrayMap1_forLoop', 1e6, () => { for (let i = 0; i < array1.length; i++) identity(array1[i]) })
|
|
133
|
+
|
|
134
|
+
// timeInLoop('arrayMap2_forLoop', 1e6, () => { for (let i = 0; i < array2.length; i++) identity(array2[i]) })
|
|
135
|
+
|
|
136
|
+
// timeInLoop('arrayMap3_forLoop', 1e6, () => { for (let i = 0; i < array3.length; i++) identity(array3[i]) })
|
|
137
|
+
|
|
138
|
+
// timeInLoop('RMap', 1e6, () => RMap(array, identity))
|
|
139
|
+
|
|
140
|
+
// timeInLoop('_Map', 1e6, () => _Map(array, identity))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const promiseAll1 = Promise.all.bind(Promise)
|
|
144
|
+
|
|
145
|
+
const promiseAll2 = array => new Promise(function (resolve) {
|
|
146
|
+
const arrayLength = array.length,
|
|
147
|
+
output = Array(arrayLength)
|
|
148
|
+
let index = -1,
|
|
149
|
+
numPromises = 0
|
|
150
|
+
while (++index < arrayLength) {
|
|
151
|
+
const value = array[index]
|
|
152
|
+
if (isPromise(value)) {
|
|
153
|
+
numPromises += 1
|
|
154
|
+
value.then((index => function(res) {
|
|
155
|
+
output[index] = res
|
|
156
|
+
numPromises -= 1
|
|
157
|
+
if (numPromises == 0) resolve(output)
|
|
158
|
+
})(index))
|
|
159
|
+
} else {
|
|
160
|
+
output[index] = value
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (numPromises == 0) resolve(object)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
{
|
|
167
|
+
/**
|
|
168
|
+
* @name promiseAll
|
|
169
|
+
*
|
|
170
|
+
* @benchmark
|
|
171
|
+
* promiseAll1: promiseArray: 1e+5: 89.714ms
|
|
172
|
+
* promiseAll2: promiseArray: 1e+5: 92.055ms
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
const normalArray = [1, 2, 3, 4, 5]
|
|
176
|
+
|
|
177
|
+
const promiseArray = [
|
|
178
|
+
Promise.resolve(1),
|
|
179
|
+
Promise.resolve(2),
|
|
180
|
+
Promise.resolve(3),
|
|
181
|
+
Promise.resolve(4),
|
|
182
|
+
Promise.resolve(5),
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
// promiseAll1(promiseArray).then(res => console.log('promiseAll1', res))
|
|
186
|
+
// promiseAll2(promiseArray).then(res => console.log('promiseAll2', res))
|
|
187
|
+
|
|
188
|
+
// timeInLoop.async('promiseAll1: promiseArray', 1e5, () => promiseAll1(promiseArray))
|
|
189
|
+
|
|
190
|
+
// timeInLoop.async('promiseAll2: promiseArray', 1e5, () => promiseAll2(promiseArray))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const promiseObjectAll1 = object => new Promise(function (resolve) {
|
|
194
|
+
const output = {}, promises = new Map()
|
|
195
|
+
for (const key in object) {
|
|
196
|
+
const value = object[key]
|
|
197
|
+
if (isPromise(value)) {
|
|
198
|
+
const promise = value.then((key => function (res) {
|
|
199
|
+
output[key] = res
|
|
200
|
+
promises.delete(key)
|
|
201
|
+
if (promises.size == 0) resolve(output)
|
|
202
|
+
})(key))
|
|
203
|
+
promises.set(key, promise)
|
|
204
|
+
} else {
|
|
205
|
+
output[key] = value
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (promises.size == 0) resolve(output)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
const promiseObjectAll2 = object => new Promise(function (resolve) {
|
|
212
|
+
const output = {}
|
|
213
|
+
let numPromises = 0
|
|
214
|
+
for (const key in object) {
|
|
215
|
+
const value = object[key]
|
|
216
|
+
if (isPromise(value)) {
|
|
217
|
+
numPromises += 1
|
|
218
|
+
value.then((key => function (res) {
|
|
219
|
+
output[key] = res
|
|
220
|
+
numPromises -= 1
|
|
221
|
+
if (numPromises == 0) resolve(output)
|
|
222
|
+
})(key))
|
|
223
|
+
} else {
|
|
224
|
+
output[key] = value
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (numPromises == 0) resolve(output)
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
const promiseObjectAll3 = object => new Promise(function (resolve) {
|
|
231
|
+
const output = {}, keys = objectKeys(object)
|
|
232
|
+
let numPromises = 0
|
|
233
|
+
for (const key of keys) {
|
|
234
|
+
const value = object[key]
|
|
235
|
+
if (isPromise(value)) {
|
|
236
|
+
numPromises += 1
|
|
237
|
+
value.then((key => function (res) {
|
|
238
|
+
output[key] = res
|
|
239
|
+
numPromises -= 1
|
|
240
|
+
if (numPromises == 0) resolve(output)
|
|
241
|
+
})(key))
|
|
242
|
+
} else {
|
|
243
|
+
output[key] = value
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (numPromises == 0) resolve(output)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
{
|
|
250
|
+
/**
|
|
251
|
+
* @name promiseObjectAll
|
|
252
|
+
*
|
|
253
|
+
* @benchmark
|
|
254
|
+
* promiseObjectAll1: promiseObject: 1e+5: 189.951ms
|
|
255
|
+
* promiseObjectAll2: promiseObject: 1e+5: 102.514ms
|
|
256
|
+
* promiseObjectAll3: promiseObject: 1e+5: 110.166ms
|
|
257
|
+
* promiseObjectAll1: normalObject: 1e+5: 68.371ms
|
|
258
|
+
* promiseObjectAll2: normalObject: 1e+5: 59.979ms
|
|
259
|
+
* promiseObjectAll3: normalObject: 1e+5: 66.123ms
|
|
260
|
+
*/
|
|
261
|
+
|
|
262
|
+
const normalObject = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
263
|
+
|
|
264
|
+
const promiseObject = {
|
|
265
|
+
a: Promise.resolve(1),
|
|
266
|
+
b: Promise.resolve(2),
|
|
267
|
+
c: Promise.resolve(3),
|
|
268
|
+
d: Promise.resolve(4),
|
|
269
|
+
e: Promise.resolve(5),
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// promiseObjectAll1(normalObject).then(res => console.log('normal', res))
|
|
273
|
+
// promiseObjectAll1(promiseObject).then(res => console.log('promise', res))
|
|
274
|
+
// promiseObjectAll2(normalObject).then(res => console.log('normal', res))
|
|
275
|
+
// promiseObjectAll2(promiseObject).then(res => console.log('promise', res))
|
|
276
|
+
// promiseObjectAll3(normalObject).then(res => console.log('normal', res))
|
|
277
|
+
// promiseObjectAll3(promiseObject).then(res => console.log('promise', res))
|
|
278
|
+
|
|
279
|
+
// timeInLoop.async('promiseObjectAll1: promiseObject', 1e5, () => promiseObjectAll1(promiseObject))
|
|
280
|
+
|
|
281
|
+
// timeInLoop.async('promiseObjectAll2: promiseObject', 1e5, () => promiseObjectAll2(promiseObject))
|
|
282
|
+
|
|
283
|
+
// timeInLoop.async('promiseObjectAll3: promiseObject', 1e5, () => promiseObjectAll3(promiseObject))
|
|
284
|
+
|
|
285
|
+
// timeInLoop.async('promiseObjectAll1: normalObject', 1e5, () => promiseObjectAll1(normalObject))
|
|
286
|
+
|
|
287
|
+
// timeInLoop.async('promiseObjectAll2: normalObject', 1e5, () => promiseObjectAll2(normalObject))
|
|
288
|
+
|
|
289
|
+
// timeInLoop.async('promiseObjectAll3: normalObject', 1e5, () => promiseObjectAll3(normalObject))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const objectMap1 = function (object, mapperFunc) {
|
|
293
|
+
const output = {}, promises = []
|
|
294
|
+
for (const key in object) {
|
|
295
|
+
const outputItem = mapperFunc(object[key])
|
|
296
|
+
if (isPromise(outputItem)) {
|
|
297
|
+
promises.push(outputItem.then(res => (output[key] = res)))
|
|
298
|
+
} else {
|
|
299
|
+
output[key] = outputItem
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return promises.length > 0 ? promiseAll(promises).then(() => output) : output
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const promiseObjectAll = promiseObjectAll2
|
|
306
|
+
|
|
307
|
+
const objectMap2 = function (object, mapperFunc) {
|
|
308
|
+
const output = {}
|
|
309
|
+
let isAsync = false
|
|
310
|
+
for (const key in object) {
|
|
311
|
+
const outputItem = mapperFunc(object[key])
|
|
312
|
+
if (isPromise(outputItem)) isAsync = true
|
|
313
|
+
output[key] = outputItem
|
|
314
|
+
}
|
|
315
|
+
return isAsync ? promiseObjectAll(output) : output
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const objectMap3 = function (object, mapperFunc) {
|
|
319
|
+
const output = {}
|
|
320
|
+
let isAsync = false
|
|
321
|
+
for (const key of objectKeys(object)) {
|
|
322
|
+
const outputItem = mapperFunc(object[key])
|
|
323
|
+
if (isPromise(outputItem)) isAsync = true
|
|
324
|
+
output[key] = outputItem
|
|
325
|
+
}
|
|
326
|
+
return isAsync ? promiseObjectAll(output) : output
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* @name objectMap
|
|
331
|
+
*
|
|
332
|
+
* @benchmark
|
|
333
|
+
* objectMap1: 1e+6: 286.133ms
|
|
334
|
+
* objectMap2: 1e+6: 184.431ms
|
|
335
|
+
* objectMap3: 1e+6: 273.79ms
|
|
336
|
+
*
|
|
337
|
+
* @remarks
|
|
338
|
+
* The stark differential between objectMap1 and objectMap2 is surprising. In neither case are the asynchronous blocks hit. Perhaps it's the additional scope introduced from the .then handler of objectMap1.
|
|
339
|
+
*/
|
|
340
|
+
|
|
341
|
+
{
|
|
342
|
+
const identity = value => value
|
|
343
|
+
|
|
344
|
+
const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
345
|
+
|
|
346
|
+
// console.log(objectMap1(object, identity))
|
|
347
|
+
// console.log(objectMap2(object, identity))
|
|
348
|
+
// console.log(objectMap3(object, identity))
|
|
349
|
+
|
|
350
|
+
// timeInLoop('objectMap1', 1e6, () => objectMap1(object, identity))
|
|
351
|
+
|
|
352
|
+
// timeInLoop('objectMap2', 1e6, () => objectMap2(object, identity))
|
|
353
|
+
|
|
354
|
+
// timeInLoop('objectMap3', 1e6, () => objectMap3(object, identity))
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const generatorFunctionMap1 = function (generatorFunc, func) {
|
|
358
|
+
return function* mappingGeneratorFunc(...args) {
|
|
359
|
+
for (const item of generatorFunc(...args)) {
|
|
360
|
+
yield func(item)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const generatorFunctionMap10 = function (generatorFunc, func) {
|
|
366
|
+
return function* mappingGeneratorFunc() {
|
|
367
|
+
for (const item of generatorFunc.apply(null, arguments)) {
|
|
368
|
+
yield func(item)
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const generatorFunctionMap11 = function (generatorFunc, func) {
|
|
374
|
+
return function* mappingGeneratorFunc(...args) {
|
|
375
|
+
for (const item of generatorFunc.apply(null, args)) {
|
|
376
|
+
yield func(item)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const generatorFunctionMap12 = function (generatorFunc, func) {
|
|
382
|
+
return function* mappingGeneratorFunc(...args) {
|
|
383
|
+
for (const item of generatorFunc.call(null, ...args)) {
|
|
384
|
+
yield func(item)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* @name generatorFunctionMap
|
|
391
|
+
*
|
|
392
|
+
* @benchmark
|
|
393
|
+
* [...squareNumbers1()]: 1e+5: 120.221ms
|
|
394
|
+
* [...squareNumbers10()]: 1e+5: 120.26ms
|
|
395
|
+
* [...squareNumbers11()]: 1e+5: 120.734ms
|
|
396
|
+
* [...squareNumbers12()]: 1e+5: 122.146ms
|
|
397
|
+
*/
|
|
398
|
+
|
|
399
|
+
{
|
|
400
|
+
const numbers = function* () {
|
|
401
|
+
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const square = number => number ** 2
|
|
405
|
+
|
|
406
|
+
const squareNumbers1 = generatorFunctionMap1(numbers, square)
|
|
407
|
+
const squareNumbers10 = generatorFunctionMap10(numbers, square)
|
|
408
|
+
const squareNumbers11 = generatorFunctionMap11(numbers, square)
|
|
409
|
+
const squareNumbers12 = generatorFunctionMap12(numbers, square)
|
|
410
|
+
|
|
411
|
+
// timeInLoop('[...squareNumbers1()]', 1e5, () => [...squareNumbers1()])
|
|
412
|
+
|
|
413
|
+
// timeInLoop('[...squareNumbers10()]', 1e5, () => [...squareNumbers10()])
|
|
414
|
+
|
|
415
|
+
// timeInLoop('[...squareNumbers11()]', 1e5, () => [...squareNumbers11()])
|
|
416
|
+
|
|
417
|
+
// timeInLoop('[...squareNumbers12()]', 1e5, () => [...squareNumbers12()])
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const iteratorMap1 = function* (iter, func) {
|
|
421
|
+
for (const item of iter) {
|
|
422
|
+
yield func(item)
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const iteratorMap2 = function (iter, func) {
|
|
427
|
+
const iterNext = iter.next.bind(iter)
|
|
428
|
+
const mappingIter = {
|
|
429
|
+
[symbolIterator]() {
|
|
430
|
+
return mappingIter
|
|
431
|
+
},
|
|
432
|
+
next() {
|
|
433
|
+
const { value, done } = iterNext()
|
|
434
|
+
return done ? { value, done } : { value: func(value), done }
|
|
435
|
+
},
|
|
436
|
+
}
|
|
437
|
+
return mappingIter
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const iteratorMap21 = function (iter, func) {
|
|
441
|
+
const mappingIter = {
|
|
442
|
+
[symbolIterator]() {
|
|
443
|
+
return mappingIter
|
|
444
|
+
},
|
|
445
|
+
next() {
|
|
446
|
+
const { value, done } = iter.next()
|
|
447
|
+
return done ? { value, done } : { value: func(value), done }
|
|
448
|
+
},
|
|
449
|
+
}
|
|
450
|
+
return mappingIter
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const iteratorMap3 = function (iter, func) {
|
|
454
|
+
const iterNext = iter.next.bind(iter)
|
|
455
|
+
const mappingIter = {
|
|
456
|
+
[symbolIterator]: () => mappingIter,
|
|
457
|
+
next: function () {
|
|
458
|
+
const { value, done } = iterNext()
|
|
459
|
+
return done ? { value, done } : { value: func(value), done }
|
|
460
|
+
},
|
|
461
|
+
}
|
|
462
|
+
return mappingIter
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const iteratorMap31 = function (iter, func) {
|
|
466
|
+
const mappingIter = {
|
|
467
|
+
[symbolIterator]: () => mappingIter,
|
|
468
|
+
next: function () {
|
|
469
|
+
const { value, done } = iter.next()
|
|
470
|
+
return done ? { value, done } : { value: func(value), done }
|
|
471
|
+
},
|
|
472
|
+
}
|
|
473
|
+
return mappingIter
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const MappingIterator = function (iter, mappingFunc) {
|
|
477
|
+
this.iter = iter
|
|
478
|
+
this.mappingFunc = mappingFunc
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
MappingIterator.prototype[symbolIterator] = function mappingValues() {
|
|
482
|
+
return this
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
MappingIterator.prototype.next = function next() {
|
|
486
|
+
const { value, done } = this.iter.next()
|
|
487
|
+
return done ? { value, done } : { value: this.mappingFunc(value), done }
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const iteratorMap5 = (iter, func) => new MappingIterator1(iter, func)
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @name iteratorMap
|
|
494
|
+
*
|
|
495
|
+
* @benchmark
|
|
496
|
+
* iteratorMap1: 1e+5: 111.57ms
|
|
497
|
+
* iteratorMap2: 1e+5: 105.097ms
|
|
498
|
+
* iteratorMap21: 1e+5: 99.13ms
|
|
499
|
+
* iteratorMap3: 1e+5: 103.8ms
|
|
500
|
+
* iteratorMap31: 1e+5: 98.715ms
|
|
501
|
+
* iteratorMap5: 1e+5: 89.505ms
|
|
502
|
+
*/
|
|
503
|
+
|
|
504
|
+
{
|
|
505
|
+
const square = number => number ** 2
|
|
506
|
+
|
|
507
|
+
const numbers = function* () {
|
|
508
|
+
yield 1; yield 2; yield 3; yield 4; yield 5
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// console.log([...numbers()])
|
|
512
|
+
// console.log([...iteratorMap1(numbers(), square)])
|
|
513
|
+
// console.log([...iteratorMap2(numbers(), square)])
|
|
514
|
+
// console.log([...iteratorMap21(numbers(), square)])
|
|
515
|
+
// console.log([...iteratorMap5(numbers(), square)])
|
|
516
|
+
|
|
517
|
+
// timeInLoop('iteratorMap1', 1e5, () => [...iteratorMap1(numbers(), square)])
|
|
518
|
+
|
|
519
|
+
// timeInLoop('iteratorMap2', 1e5, () => [...iteratorMap2(numbers(), square)])
|
|
520
|
+
|
|
521
|
+
// timeInLoop('iteratorMap21', 1e5, () => [...iteratorMap21(numbers(), square)])
|
|
522
|
+
|
|
523
|
+
// timeInLoop('iteratorMap3', 1e5, () => [...iteratorMap3(numbers(), square)])
|
|
524
|
+
|
|
525
|
+
// timeInLoop('iteratorMap31', 1e5, () => [...iteratorMap31(numbers(), square)])
|
|
526
|
+
|
|
527
|
+
// timeInLoop('iteratorMap5', 1e5, () => [...iteratorMap5(numbers(), square)])
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const asyncGeneratorFunctionMap1 = function (asyncGeneratorFunc, func) {
|
|
531
|
+
return async function* mappingAsyncGeneratorFunc(...args) {
|
|
532
|
+
for await (const item of asyncGeneratorFunc(...args)) {
|
|
533
|
+
yield func(item)
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* @name asyncGeneratorFunctionMap
|
|
540
|
+
*
|
|
541
|
+
* @benchmark
|
|
542
|
+
* asyncGeneratorFunctionMap1: 1e+5: 431.501ms
|
|
543
|
+
*/
|
|
544
|
+
|
|
545
|
+
{
|
|
546
|
+
const asyncNumbers = async function*() {
|
|
547
|
+
for (let i = 1; i <= 5; i++) yield i
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const square = number => number ** 2
|
|
551
|
+
|
|
552
|
+
const squares = asyncGeneratorFunctionMap1(asyncNumbers, square)
|
|
553
|
+
|
|
554
|
+
// asyncIteratorToArray(squares()).then(console.log)
|
|
555
|
+
|
|
556
|
+
// timeInLoop.async('asyncGeneratorFunctionMap1', 1e5, () => asyncIteratorToArray(squares()))
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
const asyncIteratorMap1 = async function* (asyncIter, func) {
|
|
560
|
+
for await (const item of asyncIter) {
|
|
561
|
+
yield func(item)
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const asyncIteratorMap2 = (asyncIter, func) => ({
|
|
566
|
+
[symbolAsyncIterator]() {
|
|
567
|
+
const asyncIterNext = asyncIter.next.bind(asyncIter)
|
|
568
|
+
return {
|
|
569
|
+
async next() {
|
|
570
|
+
const { value, done } = await asyncIterNext()
|
|
571
|
+
return done ? { value, done } : { value: await func(value), done }
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
})
|
|
576
|
+
|
|
577
|
+
const MappingAsyncIterator = function (asyncIter, mappingFunc) {
|
|
578
|
+
this.asyncIter = asyncIter
|
|
579
|
+
this.mappingFunc = mappingFunc
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
MappingAsyncIterator.prototype[symbolAsyncIterator] = function mappingValues() {
|
|
583
|
+
return this
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
MappingAsyncIterator.prototype.next = async function next() {
|
|
587
|
+
const { value, done } = await this.asyncIter.next()
|
|
588
|
+
return done ? { value, done } : { value: await this.mappingFunc(value), done }
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const asyncIteratorMap3 = (asyncIter, mappingFunc) => new MappingAsyncIterator(asyncIter, mappingFunc)
|
|
592
|
+
|
|
593
|
+
const MappingAsyncIterator2 = function (asyncIter, mappingFunc) {
|
|
594
|
+
this.asyncIter = asyncIter
|
|
595
|
+
this.mappingFunc = mappingFunc
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
MappingAsyncIterator2.prototype[symbolAsyncIterator] = function mappingValues() {
|
|
599
|
+
return this
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
MappingAsyncIterator2.prototype.next = async function next() {
|
|
603
|
+
const { value, done } = await this.asyncIter.next()
|
|
604
|
+
if (done) return { value: undefined, done: true }
|
|
605
|
+
const resultItem = this.mappingFunc(value)
|
|
606
|
+
return isPromise(resultItem)
|
|
607
|
+
? resultItem.then(res => ({ value: res, done: false }))
|
|
608
|
+
: ({ value: resultItem, done: false })
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const asyncIteratorMap31 = (asyncIter, mappingFunc) => new MappingAsyncIterator2(asyncIter, mappingFunc)
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* @name asyncIteratorMap
|
|
615
|
+
*
|
|
616
|
+
* @benchmark
|
|
617
|
+
* asyncIteratorMap1: 1e+5: 411.392ms
|
|
618
|
+
* asyncIteratorMap2: 1e+5: 422.184ms
|
|
619
|
+
* asyncIteratorMap3: 1e+5: 385.01ms
|
|
620
|
+
* asyncIteratorMap31: 1e+5: 332.436ms
|
|
621
|
+
*/
|
|
622
|
+
|
|
623
|
+
{
|
|
624
|
+
const asyncNumbers = async function*() {
|
|
625
|
+
for (let i = 1; i <= 5; i++) yield i
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const square = number => number ** 2
|
|
629
|
+
|
|
630
|
+
// timeInLoop.async('asyncIteratorMap1', 1e5, () => asyncIteratorToArray(asyncIteratorMap1(asyncNumbers(), square)))
|
|
631
|
+
|
|
632
|
+
// timeInLoop.async('asyncIteratorMap2', 1e5, () => asyncIteratorToArray(asyncIteratorMap2(asyncNumbers(), square)))
|
|
633
|
+
|
|
634
|
+
// timeInLoop.async('asyncIteratorMap3', 1e5, () => asyncIteratorToArray(asyncIteratorMap3(asyncNumbers(), square)))
|
|
635
|
+
|
|
636
|
+
// timeInLoop.async('asyncIteratorMap31', 1e5, () => asyncIteratorToArray(asyncIteratorMap31(asyncNumbers(), square)))
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
const reducerMap1 = (reducer, func) => function mappingReducer(accum, value) {
|
|
640
|
+
const resultItem = func(value)
|
|
641
|
+
return isPromise(resultItem)
|
|
642
|
+
? resultItem.then(res => reducer(accum, res))
|
|
643
|
+
: reducer(accum, resultItem)
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const reducerMap10 = (reducer, func) => function mappingReducer(accum, value) {
|
|
647
|
+
const resultItem = func.call(null, value)
|
|
648
|
+
return isPromise(resultItem)
|
|
649
|
+
? resultItem.then(res => reducer.call(null, accum, res))
|
|
650
|
+
: reducer(accum, resultItem)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* @name reducerMap
|
|
655
|
+
*
|
|
656
|
+
* @benchmark
|
|
657
|
+
* reducerMap1: 1e+5: 162.672ms
|
|
658
|
+
* reducerMap10: 1e+5: 176.32ms
|
|
659
|
+
*/
|
|
660
|
+
|
|
661
|
+
{
|
|
662
|
+
const numbers = [1, 2, 3, 4, 5]
|
|
663
|
+
|
|
664
|
+
const square = number => number ** 2
|
|
665
|
+
|
|
666
|
+
const arrayConcat = (arrA, arrB) => arrA.concat(arrB)
|
|
667
|
+
|
|
668
|
+
// timeInLoop('reducerMap1', 1e5, () => numbers.reduce(reducerMap1(arrayConcat, square), []))
|
|
669
|
+
|
|
670
|
+
// timeInLoop('reducerMap10', 1e5, () => numbers.reduce(reducerMap10(arrayConcat, square), []))
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
const possiblePromiseThen = (value, func) => (
|
|
674
|
+
isPromise(value) ? value.then(func) : func(value))
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* @name arrayMapSeries
|
|
678
|
+
*
|
|
679
|
+
* @benchmark
|
|
680
|
+
* arrayMapSeries1: 1e+5: 173.068ms
|
|
681
|
+
* arrayMapSeries2: 1e+5: 12.713ms
|
|
682
|
+
*/
|
|
683
|
+
|
|
684
|
+
const arrayMapSeries1 = (f, x, i, y) => {
|
|
685
|
+
if (i === x.length) return y
|
|
686
|
+
return possiblePromiseThen(
|
|
687
|
+
f(x[i]),
|
|
688
|
+
res => arrayMapSeries1(f, x, i + 1, y.concat(res)),
|
|
689
|
+
)
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const asyncArrayMapSeries = async function (array, func, result, index) {
|
|
693
|
+
const arrayLength = array.length
|
|
694
|
+
while (++index < arrayLength) {
|
|
695
|
+
result[index] = await func(array[index])
|
|
696
|
+
}
|
|
697
|
+
return result
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const arrayMapSeries2 = function (array, func) {
|
|
701
|
+
const arrayLength = array.length,
|
|
702
|
+
result = Array(arrayLength)
|
|
703
|
+
let index = -1
|
|
704
|
+
while (++index < arrayLength) {
|
|
705
|
+
const resultItem = func(array[index])
|
|
706
|
+
if (isPromise(resultItem)) {
|
|
707
|
+
return resultItem.then(res => {
|
|
708
|
+
result[index] = res
|
|
709
|
+
return asyncArrayMapSeries(array, func, result, index)
|
|
710
|
+
})
|
|
711
|
+
}
|
|
712
|
+
result[index] = resultItem
|
|
713
|
+
}
|
|
714
|
+
return result
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
{
|
|
718
|
+
const numbers = [1, 2, 3, 4, 5]
|
|
719
|
+
|
|
720
|
+
const square = number => number ** 2
|
|
721
|
+
|
|
722
|
+
// console.log(arrayMapSeries1(square, numbers, 0, []))
|
|
723
|
+
// console.log(arrayMapSeries2(numbers, square))
|
|
724
|
+
|
|
725
|
+
// timeInLoop('arrayMapSeries1', 1e5, () => arrayMapSeries1(square, numbers, 0, []))
|
|
726
|
+
|
|
727
|
+
// timeInLoop('arrayMapSeries2', 1e5, () => arrayMapSeries2(numbers, square))
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* @name arrayMapPool
|
|
732
|
+
*
|
|
733
|
+
* @benchmark
|
|
734
|
+
* arrayMapPool1: 1e+5: 342.172ms
|
|
735
|
+
* arrayMapPool2: 1e+5: 359.173ms
|
|
736
|
+
*/
|
|
737
|
+
|
|
738
|
+
const asyncArrayMapPool1 = async function (
|
|
739
|
+
array, mappingFunc, size, result, index, promises,
|
|
740
|
+
) {
|
|
741
|
+
const arrayLength = array.length
|
|
742
|
+
while (++index < arrayLength) {
|
|
743
|
+
if (promises.size >= size) {
|
|
744
|
+
await promiseRace(promises)
|
|
745
|
+
}
|
|
746
|
+
const resultItem = mappingFunc(array[index])
|
|
747
|
+
if (isPromise(resultItem)) {
|
|
748
|
+
const selfDeletingPromise = resultItem.then(res => {
|
|
749
|
+
promises.delete(selfDeletingPromise)
|
|
750
|
+
return res
|
|
751
|
+
})
|
|
752
|
+
promises.add(selfDeletingPromise)
|
|
753
|
+
result[index] = selfDeletingPromise
|
|
754
|
+
} else {
|
|
755
|
+
result[index] = resultItem
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return promiseAll(result)
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
const arrayMapPool1 = function (array, mappingFunc, size) {
|
|
762
|
+
const arrayLength = array.length,
|
|
763
|
+
result = Array(arrayLength)
|
|
764
|
+
let index = -1
|
|
765
|
+
while (++index < arrayLength) {
|
|
766
|
+
const resultItem = mappingFunc(array[index])
|
|
767
|
+
if (isPromise(resultItem)) {
|
|
768
|
+
const promises = new Set()
|
|
769
|
+
const selfDeletingPromise = resultItem.then(res => {
|
|
770
|
+
promises.delete(selfDeletingPromise)
|
|
771
|
+
return res
|
|
772
|
+
})
|
|
773
|
+
promises.add(selfDeletingPromise)
|
|
774
|
+
result[index] = selfDeletingPromise
|
|
775
|
+
return asyncArrayMapPool1(
|
|
776
|
+
array, mappingFunc, size, result, index, promises)
|
|
777
|
+
}
|
|
778
|
+
result[index] = resultItem
|
|
779
|
+
}
|
|
780
|
+
return result
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const setProtoAdd = Set.prototype.add
|
|
784
|
+
|
|
785
|
+
const setProtoDelete = Set.prototype.delete
|
|
786
|
+
|
|
787
|
+
const setAdd = (set, item) => setProtoAdd.call(set, item)
|
|
788
|
+
|
|
789
|
+
const setDelete = (set, item) => setProtoDelete.call(set, item)
|
|
790
|
+
|
|
791
|
+
const asyncArrayMapPool2 = async function (
|
|
792
|
+
array, mappingFunc, size, result, index, promises,
|
|
793
|
+
) {
|
|
794
|
+
const arrayLength = array.length
|
|
795
|
+
while (++index < arrayLength) {
|
|
796
|
+
if (promises.size >= size) {
|
|
797
|
+
await promiseRace(promises)
|
|
798
|
+
}
|
|
799
|
+
const resultItem = mappingFunc(array[index])
|
|
800
|
+
if (isPromise(resultItem)) {
|
|
801
|
+
const selfDeletingPromise = resultItem.then(res => {
|
|
802
|
+
setDelete(promises, selfDeletingPromise)
|
|
803
|
+
return res
|
|
804
|
+
})
|
|
805
|
+
setAdd(promises, selfDeletingPromise)
|
|
806
|
+
result[index] = selfDeletingPromise
|
|
807
|
+
} else {
|
|
808
|
+
result[index] = resultItem
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return promiseAll(result)
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const arrayMapPool2 = function (array, mappingFunc, size) {
|
|
815
|
+
const arrayLength = array.length,
|
|
816
|
+
result = Array(arrayLength)
|
|
817
|
+
let index = -1
|
|
818
|
+
while (++index < arrayLength) {
|
|
819
|
+
const resultItem = mappingFunc(array[index])
|
|
820
|
+
if (isPromise(resultItem)) {
|
|
821
|
+
const promises = new Set()
|
|
822
|
+
const selfDeletingPromise = resultItem.then(res => {
|
|
823
|
+
promises.delete(selfDeletingPromise)
|
|
824
|
+
return res
|
|
825
|
+
})
|
|
826
|
+
promises.add(selfDeletingPromise)
|
|
827
|
+
result[index] = selfDeletingPromise
|
|
828
|
+
return asyncArrayMapPool2(
|
|
829
|
+
array, mappingFunc, size, result, index, promises)
|
|
830
|
+
}
|
|
831
|
+
result[index] = resultItem
|
|
832
|
+
}
|
|
833
|
+
return result
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
{
|
|
837
|
+
const asyncSquare = async number => number ** 2
|
|
838
|
+
|
|
839
|
+
const range = length => Array.from({ length }, (_, i) => i + 1)
|
|
840
|
+
|
|
841
|
+
const promiseRange = length => Array.from({ length }, (_, i) => Promise.resolve(i))
|
|
842
|
+
|
|
843
|
+
// arrayMapPool1(range(5), asyncSquare, 100).then(console.log)
|
|
844
|
+
// arrayMapPool2(range(5), asyncSquare, 100).then(console.log)
|
|
845
|
+
|
|
846
|
+
// timeInLoop.async('arrayMapPool1', 1e5, () => arrayMapPool1(range(5), asyncSquare, 100))
|
|
847
|
+
// timeInLoop.async('arrayMapPool2', 1e5, () => arrayMapPool2(range(5), asyncSquare, 100))
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* @name arrayMapWithIndex
|
|
852
|
+
*
|
|
853
|
+
* @benchmark
|
|
854
|
+
* arrayMapWithIndex1: 1e+6: 29.659ms
|
|
855
|
+
* arrayMapWithIndex2: 1e+6: 31.844ms
|
|
856
|
+
*/
|
|
857
|
+
|
|
858
|
+
const arrayMapWithIndex1 = function (array, func) {
|
|
859
|
+
const arrayLength = array.length,
|
|
860
|
+
result = Array(arrayLength)
|
|
861
|
+
let index = -1, isAsync = false
|
|
862
|
+
while (++index < arrayLength) {
|
|
863
|
+
const resultItem = func(array[index], index, array)
|
|
864
|
+
if (isPromise(resultItem)) isAsync = true
|
|
865
|
+
result[index] = resultItem
|
|
866
|
+
}
|
|
867
|
+
return isAsync ? promiseAll(result) : result
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
const arrayMapWithIndex2 = function (array, func) {
|
|
871
|
+
const arrayLength = array.length,
|
|
872
|
+
result = Array(arrayLength)
|
|
873
|
+
let index = -1, isAsync = false
|
|
874
|
+
while (++index < arrayLength) {
|
|
875
|
+
const resultItem = func.call(null, array[index], index, array)
|
|
876
|
+
if (isPromise(resultItem)) isAsync = true
|
|
877
|
+
result[index] = resultItem
|
|
878
|
+
}
|
|
879
|
+
return isAsync ? promiseAll(result) : result
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
{
|
|
883
|
+
const array = [1, 2, 3, 4, 5]
|
|
884
|
+
|
|
885
|
+
const identity = value => value
|
|
886
|
+
|
|
887
|
+
// console.log(arrayMapWithIndex1(array, identity))
|
|
888
|
+
|
|
889
|
+
// timeInLoop('arrayMapWithIndex1', 1e6, () => arrayMapWithIndex1(array, identity))
|
|
890
|
+
|
|
891
|
+
// timeInLoop('arrayMapWithIndex2', 1e6, () => arrayMapWithIndex2(array, identity))
|
|
892
|
+
}
|