我目前正在测试在PouchDB中插入多个不同的对象所需的时间,要么一个接一个地插入它们,要么执行批量插入。我的方法是检索一个大的生成的JSON项,并在循环中使用一个自定义函数更改_id,然后将执行插入的函数推送到承诺数组中,以最终调用数组上的Promise.all。
测试本质上是异步的(需要延迟关键字)。
代码看起来像(coffeescript)
benchmarkCreate: () ->
result = {}
Ember.$.getJSON('bigitem.json').then((doc) =>
@createUpdateTest 10, result, doc[0] # Takes the JSON only
)
createUpdateTest: (many, res, doc) ->
(@get 'benchSuite').add(new Benchmark("Create #{many} Items Bench",
'defer': true
fn: (deferred) =>
iterations = []
i = 0
while i < many
doc._id = @makeid()
console.log 'Out of the Promise array: ' + doc._id
iterations.push ((@get 'pouchService').createUpdateDoc doc)
i++
Promise.all(iterations).then((response) ->
deferred.resolve()
)
))假定pouchService可以成功地插入具有不同id的新项(不需要修改管理)。在将文档放入PouchDB之前,我还有另一个打印输出:
Out of the Promise array: z2sF0
Out of the Promise array: v2J3F
Out of the Promise array: MY2qX
Out of the Promise array: BkKiv
Out of the Promise array: DjcUK
Out of the Promise array: TIL6e
Out of the Promise array: xjz20
Out of the Promise array: oHAFf
Out of the Promise array: dWK6U
Out of the Promise array: 9KKRi
Inside the Promise Array: 9KKRi
Inside the Promise Array: 9KKRi
Inside the Promise Array: 9KKRi
... X 10在推入承诺数组之前,我需要修改超过id 10次,但它似乎只使用最后一个值,即使它在推送函数之前打印了新的值。
发布于 2016-01-05 13:56:22
我只是解决了我自己的问题以防有人感兴趣。当为文档分配一个新的id时,它是对象引用被修改的对象,而不是新的对象。要解决这个问题:
docAux = Ember.copy doc
Object.assign docAux, { _id: @makeid() } # New id for each new item.https://stackoverflow.com/questions/34608565
复制相似问题