Skip to content

Commit

Permalink
Merge pull request #14762 from Automattic/vkarpov15/perf-clone-primit…
Browse files Browse the repository at this point in the history
…ives

perf(clone): avoid further unnecessary checks if cloning a primitive value
  • Loading branch information
vkarpov15 authored Jul 26, 2024
2 parents 93684f8 + ad75131 commit f921c9a
Showing 1 changed file with 7 additions and 5 deletions.
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

0 comments on commit f921c9a

Please sign in to comment.