Skip to content

Commit

Permalink
perf: avoid unnecesary get() call and use faster approach for convert…
Browse files Browse the repository at this point in the history
…ing to string

Re: #14394
  • Loading branch information
vkarpov15 committed Jun 17, 2024
1 parent 332f7f5 commit 2991d7b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4066,7 +4066,8 @@ Document.prototype.toObject = function(options) {

function applyVirtuals(self, json, options, toObjectOptions) {
const schema = self.$__schema;
const paths = Object.keys(schema.virtuals);
const virtuals = schema.virtuals;
const paths = Object.keys(virtuals);
let i = paths.length;
const numPaths = i;
let path;
Expand Down Expand Up @@ -4117,7 +4118,7 @@ function applyVirtuals(self, json, options, toObjectOptions) {
assignPath = path.substring(options.path.length + 1);
}
if (assignPath.indexOf('.') === -1 && assignPath === path) {
v = self.get(path, null, { noDottedPath: true });
v = virtuals[path].applyGetters(void 0, self);
v = clone(v, options);
if (v === void 0) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/schema/idGetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function addIdGetter(schema) {

function idGetter() {
if (this._id != null) {
return String(this._id);
return this._id.toString();
}

return null;
Expand Down

0 comments on commit 2991d7b

Please sign in to comment.