Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(clone): avoid further unnecessary checks if cloning a primitive value #14762

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/helpers/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const trustedSymbol = require('./query/trusted').trustedSymbol;
*
* If options.minimize is true, creates a minimal data object. Empty objects and undefined values will not be cloned. This makes the data payload sent to MongoDB as small as possible.
*
* Functions are never cloned.
* Functions and primitives are never cloned.
*
* @param {Object} obj the object to clone
* @param {Object} options
Expand All @@ -30,6 +30,9 @@ function clone(obj, options, isArrayChild) {
if (obj == null) {
return obj;
}
if (typeof obj === 'number' || typeof obj === 'string' || typeof obj === 'boolean' || typeof obj === 'bigint') {
return obj;
}

if (Array.isArray(obj)) {
return cloneArray(isMongooseArray(obj) ? obj.__array : obj, options);
Expand Down Expand Up @@ -148,13 +151,12 @@ function cloneObject(obj, options, isArrayChild) {
ret[trustedSymbol] = obj[trustedSymbol];
}

let i = 0;
let key = '';
const keys = Object.keys(obj);
const len = keys.length;

for (i = 0; i < len; ++i) {
if (specialProperties.has(key = keys[i])) {
for (let i = 0; i < len; ++i) {
const key = keys[i];
if (specialProperties.has(key)) {
continue;
}

Expand Down
Loading