mongoid/mongoid — Issue #1379: performance hit on first access to an array of embedded objects
Author: rcbth
Created: 2011-10-26 18:51:53+00:00
State: open

Given a document "Set" that has N embedded "Thing" documents I've found that the first access to the array of embedded objects was several orders of magnitude slower than successive ones.

```ruby
set.things.first.member # slow
set.things.first.member # much, much faster
```

The really strange thing from my point of view was that accessing the object via BSON did not cause a similar slowdown.

```ruby
set['things'][0]['member'] # perfectly fast
set['things'][0]['member'] # just as fast
```

Running a test over thousands of objects I found the mongoid first access to be about **100x slower** than either subsequent mongoid accesses or the BSON method. Perhaps there is some nasty initialization that takes place on the first run? Thanks!