mongoose: Severe performance regression in 3.8.14

The fix for #2140 (cdd63bf6ff0854018cd6b70fd8397c9e942dcd59) introduced a quite severe performance regression.

Compare the runtime of this code sample with mongoose 3.8.13 and 4.7.2:

var mongoose = require('mongoose')
var fs = require('fs')

var Schema = mongoose.Schema;

ChecklistSchema = new Schema({
    name: { type: String }
});

BoardSchema = new Schema({
    checklists: { type: [ChecklistSchema] }
});

var Board = mongoose.model('Board', BoardSchema);

var doc = new Board();
for (var i = 0; i < 1000; ++i) {
    var checklist = {name:'test'};
    doc.checklists.push(checklist);
}

var obj = doc.toObject();
var time2 = 0;
var start2 = new Date();
for (var i = 0; i < 100; i++) {
  new Board(obj);
}
console.error('instantiating schema object took %d ms', (new Date - start2));
sebastian@host:~/Desktop/test$ node big # mongoose 3.8.13
instantiating schema object took 1127 ms
sebastian@host:~/Desktop/test$ node big # mongoose 4.7.3
instantiating schema object took 6898 ms

So at this point I think itโ€™d be better to either make .id properly handle a null subdoc, or alternatively loop through all subdocs before calling id and checking for null first (and then avoid calling .id if any are found).

Node v6.9.2 mongoose 4.7.3

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Using the head of this branch as it stands right now is now much faster than 3.8.40, will release that as part of 4.8

$ node gh-4821.js 
instantiating schema object took 2337 ms
$
$ rm -rf node_modules/mongoose && npm install mongoose@3.8.40
$ node gh-4821.js 
js-bson: Failed to load c++ bson extension, using pure JS version
instantiating schema object took 4490 ms
$ 

๐Ÿš€