_clone.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import _cloneRegExp from "./_cloneRegExp.js";
  2. import type from "../type.js";
  3. /**
  4. * Copies an object.
  5. *
  6. * @private
  7. * @param {*} value The value to be copied
  8. * @param {Boolean} deep Whether or not to perform deep cloning.
  9. * @return {*} The copied value.
  10. */
  11. export default function _clone(value, deep, map) {
  12. map || (map = new _ObjectMap()); // this avoids the slower switch with a quick if decision removing some milliseconds in each run.
  13. if (_isPrimitive(value)) {
  14. return value;
  15. }
  16. var copy = function copy(copiedValue) {
  17. // Check for circular and same references on the object graph and return its corresponding clone.
  18. var cachedCopy = map.get(value);
  19. if (cachedCopy) {
  20. return cachedCopy;
  21. }
  22. map.set(value, copiedValue);
  23. for (var key in value) {
  24. if (Object.prototype.hasOwnProperty.call(value, key)) {
  25. copiedValue[key] = deep ? _clone(value[key], true, map) : value[key];
  26. }
  27. }
  28. return copiedValue;
  29. };
  30. switch (type(value)) {
  31. case 'Object':
  32. return copy(Object.create(Object.getPrototypeOf(value)));
  33. case 'Array':
  34. return copy([]);
  35. case 'Date':
  36. return new Date(value.valueOf());
  37. case 'RegExp':
  38. return _cloneRegExp(value);
  39. case 'Int8Array':
  40. case 'Uint8Array':
  41. case 'Uint8ClampedArray':
  42. case 'Int16Array':
  43. case 'Uint16Array':
  44. case 'Int32Array':
  45. case 'Uint32Array':
  46. case 'Float32Array':
  47. case 'Float64Array':
  48. case 'BigInt64Array':
  49. case 'BigUint64Array':
  50. return value.slice();
  51. default:
  52. return value;
  53. }
  54. }
  55. function _isPrimitive(param) {
  56. var type = typeof param;
  57. return param == null || type != 'object' && type != 'function';
  58. }
  59. var _ObjectMap =
  60. /*#__PURE__*/
  61. function () {
  62. function _ObjectMap() {
  63. this.map = {};
  64. this.length = 0;
  65. }
  66. _ObjectMap.prototype.set = function (key, value) {
  67. const hashedKey = this.hash(key);
  68. let bucket = this.map[hashedKey];
  69. if (!bucket) {
  70. this.map[hashedKey] = bucket = [];
  71. }
  72. bucket.push([key, value]);
  73. this.length += 1;
  74. };
  75. _ObjectMap.prototype.hash = function (key) {
  76. let hashedKey = [];
  77. for (var value in key) {
  78. hashedKey.push(Object.prototype.toString.call(key[value]));
  79. }
  80. return hashedKey.join();
  81. };
  82. _ObjectMap.prototype.get = function (key) {
  83. /**
  84. * depending on the number of objects to be cloned is faster to just iterate over the items in the map just because the hash function is so costly,
  85. * on my tests this number is 180, anything above that using the hash function is faster.
  86. */
  87. if (this.length <= 180) {
  88. for (const p in this.map) {
  89. const bucket = this.map[p];
  90. for (let i = 0; i < bucket.length; i += 1) {
  91. const element = bucket[i];
  92. if (element[0] === key) {
  93. return element[1];
  94. }
  95. }
  96. }
  97. return;
  98. }
  99. const hashedKey = this.hash(key);
  100. const bucket = this.map[hashedKey];
  101. if (!bucket) {
  102. return;
  103. }
  104. for (let i = 0; i < bucket.length; i += 1) {
  105. const element = bucket[i];
  106. if (element[0] === key) {
  107. return element[1];
  108. }
  109. }
  110. };
  111. return _ObjectMap;
  112. }();