0

I am currently testing the time it takes to insert a number of different objects in PouchDB either inserting them one-by-one or performing a bulk insert. My approach is retrieving a big generated JSON item and changing the _id using a custom function inside a loop, and then pushing the function that will perform the insert into a promises array to finally call Promise.all over the array.

Tests are inherently asynchronous (defer keyword needed).

Code looks like (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 is assumed to successfully insert new items with different id's (no revision managing required). I have another print just before putting the document in PouchDB and output I have is:

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

I need to modify than id 10 times before pushing into the promise array but it seems to take only the last value, even when it prints the new one just before pushing the function.

Urco
  • 365
  • 3
  • 14

1 Answers1

0

I just solved my own problem in case someone might be interested. When assigning a new id to the document it was the object reference what was being modified, not a new object. To solve it:

docAux = Ember.copy doc
Object.assign docAux, { _id: @makeid() } # New id for each new item.
Urco
  • 365
  • 3
  • 14