@contrail/util 1.1.14-alpha → 1.1.14-alpha-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,32 @@
1
+ type PerformanceComparisonEvent = {
2
+ durationMsJson: number;
3
+ durationMsRFDC: number;
4
+ input: any;
5
+ };
6
+ type PerformanceSummary = {
7
+ eventForWhichJsonWasMostFaster: PerformanceComparisonEvent | null;
8
+ eventForWhichRFDCWasMostFaster: PerformanceComparisonEvent | null;
9
+ totalDurationMsJson: number;
10
+ totalDurationMsRFDC: number;
11
+ invocationsInWhichJsonWasFaster: PerformanceComparisonEvent[];
12
+ invocationsInWhichRFDCWasFaster: PerformanceComparisonEvent[];
13
+ };
14
+ type CloneMethods = {
15
+ cloneRfdc: <T>(obj: T) => T;
16
+ cloneJson: <T>(obj: T) => T;
17
+ withTiming: (fn: () => any) => {
18
+ result: any;
19
+ durationMs: number;
20
+ };
21
+ };
22
+ declare global {
23
+ interface Window {
24
+ performanceSummary: PerformanceSummary;
25
+ performanceEvents: PerformanceComparisonEvent[];
26
+ cloneMethods: CloneMethods;
27
+ }
28
+ }
1
29
  export declare function cloneDeep<T>(obj: T, options?: {
2
30
  shouldPreserveCircularReferences?: boolean;
3
31
  }): T;
32
+ export {};
@@ -3,9 +3,66 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cloneDeep = cloneDeep;
4
4
  const cloneDefault = require('rfdc')();
5
5
  const cloneWithCircles = require('rfdc')({ circles: true });
6
- function cloneDeep(obj, options) {
6
+ function cloneRfdc(obj, options) {
7
7
  var _a;
8
8
  const shouldPreserveCircles = (_a = options === null || options === void 0 ? void 0 : options.shouldPreserveCircularReferences) !== null && _a !== void 0 ? _a : false;
9
9
  const clone = shouldPreserveCircles ? cloneWithCircles : cloneDefault;
10
10
  return clone(obj);
11
11
  }
12
+ function withTiming(fn) {
13
+ const start = performance.now();
14
+ const result = fn();
15
+ const durationMs = performance.now() - start;
16
+ return { result, durationMs };
17
+ }
18
+ if (typeof window === 'undefined') {
19
+ var window = {};
20
+ }
21
+ window.cloneMethods = {
22
+ cloneRfdc,
23
+ cloneJson: (obj) => JSON.parse(JSON.stringify(obj)),
24
+ withTiming,
25
+ };
26
+ function cloneDeep(obj, options) {
27
+ var _a;
28
+ const shouldPreserveCircles = (_a = options === null || options === void 0 ? void 0 : options.shouldPreserveCircularReferences) !== null && _a !== void 0 ? _a : false;
29
+ const clone = shouldPreserveCircles ? cloneWithCircles : cloneDefault;
30
+ const { durationMs: durationMsJson } = withTiming(() => JSON.parse(JSON.stringify(obj)));
31
+ const { durationMs: durationMsRFDC } = withTiming(() => clone(obj));
32
+ const rfdcClone = clone(obj);
33
+ if (!window.performanceEvents) {
34
+ window.performanceEvents = [];
35
+ }
36
+ const performanceEvent = {
37
+ durationMsJson,
38
+ durationMsRFDC,
39
+ input: obj,
40
+ };
41
+ window.performanceEvents.push(performanceEvent);
42
+ window.performanceSummary = window.performanceSummary || {
43
+ eventForWhichJsonWasMostFaster: null,
44
+ eventForWhichRFDCWasMostFaster: null,
45
+ totalDurationMsJson: 0,
46
+ totalDurationMsRFDC: 0,
47
+ invocationsInWhichJsonWasFaster: [],
48
+ invocationsInWhichRFDCWasFaster: [],
49
+ };
50
+ window.performanceSummary.totalDurationMsJson += durationMsJson;
51
+ window.performanceSummary.totalDurationMsRFDC += durationMsRFDC;
52
+ const isJsonFaster = durationMsJson < durationMsRFDC;
53
+ if (isJsonFaster) {
54
+ window.performanceSummary.invocationsInWhichJsonWasFaster.push(performanceEvent);
55
+ if (!window.performanceSummary.eventForWhichJsonWasMostFaster ||
56
+ durationMsJson < window.performanceSummary.eventForWhichJsonWasMostFaster.durationMsJson) {
57
+ window.performanceSummary.eventForWhichJsonWasMostFaster = performanceEvent;
58
+ }
59
+ }
60
+ else {
61
+ window.performanceSummary.invocationsInWhichRFDCWasFaster.push(performanceEvent);
62
+ if (!window.performanceSummary.eventForWhichRFDCWasMostFaster ||
63
+ durationMsRFDC < window.performanceSummary.eventForWhichRFDCWasMostFaster.durationMsRFDC) {
64
+ window.performanceSummary.eventForWhichRFDCWasMostFaster = performanceEvent;
65
+ }
66
+ }
67
+ return rfdcClone;
68
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lodash_1 = require("lodash");
4
+ const cloneDeep_1 = require("./cloneDeep");
5
+ function jsonClone(obj) {
6
+ return JSON.parse(JSON.stringify(obj));
7
+ }
8
+ console.log('Performance test for small objects:');
9
+ let smallObject = {
10
+ sub1: {
11
+ val: 1,
12
+ },
13
+ sub3: {
14
+ val: 2,
15
+ valX: '3',
16
+ },
17
+ };
18
+ console.time('cloneDeepSmallRFDC');
19
+ Array.from({ length: 10000 }).forEach(() => {
20
+ (0, cloneDeep_1.cloneDeep)(smallObject);
21
+ });
22
+ console.timeEnd('cloneDeepSmallRFDC');
23
+ console.time('cloneDeepSmallLodash');
24
+ Array.from({ length: 10000 }).forEach(() => {
25
+ (0, lodash_1.cloneDeep)(smallObject);
26
+ });
27
+ console.timeEnd('cloneDeepSmallLodash');
28
+ console.time('cloneDeepSmallJson');
29
+ Array.from({ length: 10000 }).forEach(() => {
30
+ jsonClone(smallObject);
31
+ });
32
+ console.timeEnd('cloneDeepSmallJson');
33
+ console.time('structuredCloneSmall');
34
+ Array.from({ length: 10000 }).forEach(() => {
35
+ structuredClone(smallObject);
36
+ });
37
+ console.timeEnd('structuredCloneSmall');
38
+ console.log('\n\nPerformance test for very large objects:');
39
+ let veryLargeObject = {};
40
+ for (let i = 0; i < 100000; i++) {
41
+ veryLargeObject[`key${i}`] = {
42
+ sub1: {
43
+ val: i,
44
+ },
45
+ sub2: new Date('2023-10-01T12:00:00.000Z'),
46
+ sub3: {
47
+ val: i + 1,
48
+ valX: i + 2,
49
+ },
50
+ };
51
+ }
52
+ console.time('cloneDeepRFDC');
53
+ (0, cloneDeep_1.cloneDeep)(veryLargeObject);
54
+ console.timeEnd('cloneDeepRFDC');
55
+ console.time('cloneDeepLodash');
56
+ (0, lodash_1.cloneDeep)(veryLargeObject);
57
+ console.timeEnd('cloneDeepLodash');
58
+ console.time('cloneDeepJson');
59
+ jsonClone(veryLargeObject);
60
+ console.timeEnd('cloneDeepJson');
61
+ console.time('structuredClone');
62
+ structuredClone(veryLargeObject);
63
+ console.timeEnd('structuredClone');
64
+ console.log('\n\nPerformance test for very large arrays:');
65
+ let veryLargeArray = [];
66
+ for (let i = 0; i < 100000; i++) {
67
+ veryLargeArray.push({
68
+ sub1: {
69
+ val: i,
70
+ },
71
+ sub2: new Date('2023-10-01T12:00:00.000Z'),
72
+ sub3: {
73
+ val: i + 1,
74
+ valX: i + 2,
75
+ },
76
+ });
77
+ }
78
+ console.time('cloneDeepArrayRFDC');
79
+ (0, cloneDeep_1.cloneDeep)(veryLargeArray);
80
+ console.timeEnd('cloneDeepArrayRFDC');
81
+ console.time('cloneDeepArrayLodash');
82
+ (0, lodash_1.cloneDeep)(veryLargeArray);
83
+ console.timeEnd('cloneDeepArrayLodash');
84
+ console.time('cloneDeepArrayJson');
85
+ jsonClone(veryLargeArray);
86
+ console.timeEnd('cloneDeepArrayJson');
87
+ console.time('structuredCloneArray');
88
+ structuredClone(veryLargeArray);
89
+ console.timeEnd('structuredCloneArray');
90
+ console.log('\n\nPerformance test for assortment.json:');
91
+ const fs = require("fs");
92
+ const path = require("path");
93
+ const assortmentFilePath = path.join(__dirname, 'assortment.json');
94
+ const assortmentData = JSON.parse(fs.readFileSync(assortmentFilePath, 'utf8'));
95
+ console.time('cloneDeepAssortment25kRFDC');
96
+ (0, cloneDeep_1.cloneDeep)(assortmentData);
97
+ console.timeEnd('cloneDeepAssortment25kRFDC');
98
+ console.time('cloneDeepAssortment25kLodash');
99
+ (0, lodash_1.cloneDeep)(assortmentData);
100
+ console.timeEnd('cloneDeepAssortment25kLodash');
101
+ console.time('cloneDeepAssortment25kJson');
102
+ jsonClone(assortmentData);
103
+ console.timeEnd('cloneDeepAssortment25kJson');
104
+ console.time('structuredCloneAssortment25k');
105
+ structuredClone(assortmentData);
106
+ console.timeEnd('structuredCloneAssortment25k');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.14-alpha",
3
+ "version": "1.1.14-alpha-2",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",