gx
chenyc
2025-02-12 ea42ff3ebee1eeb3fb29423aa848a249441db81c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*!
 * Module dependencies.
 */
 
'use strict';
 
const CoreMongooseArray = require('./core_array');
 
const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
const arrayParentSymbol = require('../helpers/symbols').arrayParentSymbol;
const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
const arraySchemaSymbol = require('../helpers/symbols').arraySchemaSymbol;
 
const _basePush = Array.prototype.push;
 
/**
 * Mongoose Array constructor.
 *
 * ####NOTE:
 *
 * _Values always have to be passed to the constructor to initialize, otherwise `MongooseArray#push` will mark the array as modified._
 *
 * @param {Array} values
 * @param {String} path
 * @param {Document} doc parent document
 * @api private
 * @inherits Array
 * @see http://bit.ly/f6CnZU
 */
 
function MongooseArray(values, path, doc, schematype) {
  let arr;
 
  if (Array.isArray(values)) {
    const len = values.length;
 
    // Perf optimizations for small arrays: much faster to use `...` than `for` + `push`,
    // but large arrays may cause stack overflows. And for arrays of length 0/1, just
    // modifying the array is faster. Seems small, but adds up when you have a document
    // with thousands of nested arrays.
    if (len === 0) {
      arr = new CoreMongooseArray();
    } else if (len === 1) {
      arr = new CoreMongooseArray(1);
      arr[0] = values[0];
    } else if (len < 10000) {
      arr = new CoreMongooseArray();
      _basePush.apply(arr, values);
    } else {
      arr = new CoreMongooseArray();
      for (let i = 0; i < len; ++i) {
        _basePush.call(arr, values[i]);
      }
    }
 
    if (values[arrayAtomicsSymbol] != null) {
      arr[arrayAtomicsSymbol] = values[arrayAtomicsSymbol];
    }
  } else {
    arr = new CoreMongooseArray();
  }
 
  arr[arrayPathSymbol] = path;
 
  // Because doc comes from the context of another function, doc === global
  // can happen if there was a null somewhere up the chain (see #3020)
  // RB Jun 17, 2015 updated to check for presence of expected paths instead
  // to make more proof against unusual node environments
  if (doc != null && doc.$__ != null) {
    arr[arrayParentSymbol] = doc;
    arr[arraySchemaSymbol] = schematype || doc.schema.path(path);
  }
 
  return arr;
}
 
/*!
 * Module exports.
 */
 
module.exports = exports = MongooseArray;