Skip to content

Commit 8b285a1

Browse files
authored
Merge pull request #14120 from Automattic/vkarpov15/gh-11380
Perf improvements when creating arrays of primitives
2 parents 699af5f + 7d26ad2 commit 8b285a1

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

lib/statemachine.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ StateMachine.ctor = function() {
6565
*/
6666

6767
StateMachine.prototype._changeState = function _changeState(path, nextState) {
68-
const prevBucket = this.states[this.paths[path]];
68+
const prevState = this.paths[path];
69+
if (prevState === nextState) {
70+
return;
71+
}
72+
const prevBucket = this.states[prevState];
6973
if (prevBucket) delete prevBucket[path];
7074

7175
this.paths[path] = nextState;

lib/types/array/methods/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,15 @@ const methods = {
374374
if (val != null && utils.hasUserDefinedProperty(val, '$each')) {
375375
atomics.$push = val;
376376
} else {
377-
atomics.$push.$each = atomics.$push.$each.concat(val);
377+
if (val.length === 1) {
378+
atomics.$push.$each.push(val[0]);
379+
} else if (val.length < 10000) {
380+
atomics.$push.$each.push(...val);
381+
} else {
382+
for (const v of val) {
383+
atomics.$push.$each.push(v);
384+
}
385+
}
378386
}
379387
} else {
380388
atomics[op] = val;
@@ -403,8 +411,7 @@ const methods = {
403411
addToSet() {
404412
_checkManualPopulation(this, arguments);
405413

406-
let values = [].map.call(arguments, this._mapCast, this);
407-
values = this[arraySchemaSymbol].applySetters(values, this[arrayParentSymbol]);
414+
const values = [].map.call(arguments, this._mapCast, this);
408415
const added = [];
409416
let type = '';
410417
if (values[0] instanceof ArraySubdocument) {
@@ -415,7 +422,7 @@ const methods = {
415422
type = 'ObjectId';
416423
}
417424

418-
const rawValues = utils.isMongooseArray(values) ? values.__array : this;
425+
const rawValues = utils.isMongooseArray(values) ? values.__array : values;
419426
const rawArray = utils.isMongooseArray(this) ? this.__array : this;
420427

421428
rawValues.forEach(function(v) {
@@ -682,10 +689,7 @@ const methods = {
682689

683690
_checkManualPopulation(this, values);
684691

685-
const parent = this[arrayParentSymbol];
686692
values = [].map.call(values, this._mapCast, this);
687-
values = this[arraySchemaSymbol].applySetters(values, parent, undefined,
688-
undefined, { skipDocumentArrayCast: true });
689693
let ret;
690694
const atomics = this[arrayAtomicsSymbol];
691695
this._markModified();
@@ -711,7 +715,7 @@ const methods = {
711715
'with different `$position`');
712716
}
713717
atomic = values;
714-
ret = [].push.apply(arr, values);
718+
ret = _basePush.apply(arr, values);
715719
}
716720

717721
this._registerAtomic('$push', atomic);
@@ -917,7 +921,6 @@ const methods = {
917921
values = arguments;
918922
} else {
919923
values = [].map.call(arguments, this._cast, this);
920-
values = this[arraySchemaSymbol].applySetters(values, this[arrayParentSymbol]);
921924
}
922925

923926
const arr = utils.isMongooseArray(this) ? this.__array : this;

test/types.array.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,4 +1915,26 @@ describe('types array', function() {
19151915
}
19161916
});
19171917
});
1918+
1919+
it('calls array setters (gh-11380)', function() {
1920+
let called = 0;
1921+
const Test = db.model('Test', new Schema({
1922+
intArr: [{
1923+
type: Number,
1924+
set: v => {
1925+
++called;
1926+
return Math.floor(v);
1927+
}
1928+
}]
1929+
}));
1930+
1931+
assert.equal(called, 0);
1932+
const doc = new Test({ intArr: [3.14] });
1933+
assert.deepStrictEqual(doc.intArr, [3]);
1934+
assert.equal(called, 1);
1935+
1936+
doc.intArr.push(2.718);
1937+
assert.deepStrictEqual(doc.intArr, [3, 2]);
1938+
assert.equal(called, 2);
1939+
});
19181940
});

0 commit comments

Comments
 (0)