-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotcha2.js
37 lines (29 loc) · 1.01 KB
/
gotcha2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const _ = require('lodash')
const DataPoint = require('data-point')
const { map } = DataPoint.helpers
// the reducer helpers can be inefficient when the operation does not need to be async
// in this case, "model:async" and "model:sync" return the same value, but the sync
// version is likely to execute faster
// async vs sync
const dataPoint = DataPoint.create({
entities: {
inputType: 'schema:a.1.0',
// this will create a promise for each element of the input array,
// which is not efficient because the operation can be done synchronously
'model:async': {
value: map('$a')
},
// this will execute faster than "model:async"
'model:sync': {
value: input => _.map(input, 'a')
}
}
})
const input = [{ a: 1 }, { a: 2 }, { a: 3 }]
const log = (type, output) => {
console.log(`${type}: ${output}`)
}
Promise.all([
dataPoint.resolve('model:async', input).then(output => log('async', output)),
dataPoint.resolve('model:sync', input).then(output => log('sync', output))
])