There's one very easy optimization you can do if the objects you create with {mid,user,message}
are immutable:
record = {mid,user,message}
ld.forEach smallDB, (a)->
ld.forEach a.locations, (b)->
b.messages.push record
If you do this, you save yourself the cost of creating a new object each time you push. Dropping the forEach
in favor of using for
would also make things faster.
It would be further optimizable by allowing a more direct access to the arrays that you access as b.messages
in your original code, but this would complicate addition and removal of users or locations. I don't think is worth it unless proved by profiling that it is not a premature optimization.