@contrail/util 1.1.13 → 1.1.14-alpha-1
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.
- package/lib/object-util/cloneDeep/cloneDeep.d.ts +23 -1
- package/lib/object-util/cloneDeep/cloneDeep.js +48 -10
- package/lib/object-util/cloneDeep/performanceTest.d.ts +1 -0
- package/lib/object-util/cloneDeep/performanceTest.js +76 -0
- package/lib/object-util/retainOnlyProperties/retainOnlyProperties.d.ts +4 -2
- package/lib/object-util/retainOnlyProperties/retainOnlyProperties.js +5 -1
- package/package.json +3 -2
|
@@ -1 +1,23 @@
|
|
|
1
|
-
|
|
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
|
+
declare global {
|
|
15
|
+
interface Window {
|
|
16
|
+
performanceSummary: PerformanceSummary;
|
|
17
|
+
performanceEvents: PerformanceComparisonEvent[];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export declare function cloneDeep<T>(obj: T, options?: {
|
|
21
|
+
shouldPreserveCircularReferences?: boolean;
|
|
22
|
+
}): T;
|
|
23
|
+
export {};
|
|
@@ -1,15 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cloneDeep = cloneDeep;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
const cloneDefault = require('rfdc')();
|
|
5
|
+
const cloneWithCircles = require('rfdc')({ circles: true });
|
|
6
|
+
function cloneDeep(obj, options) {
|
|
7
|
+
var _a;
|
|
8
|
+
console.log('running cloneDeep');
|
|
9
|
+
const shouldPreserveCircles = (_a = options === null || options === void 0 ? void 0 : options.shouldPreserveCircularReferences) !== null && _a !== void 0 ? _a : false;
|
|
10
|
+
const clone = shouldPreserveCircles ? cloneWithCircles : cloneDefault;
|
|
11
|
+
const startJson = performance.now();
|
|
12
|
+
JSON.parse(JSON.stringify(obj));
|
|
13
|
+
const durationMsJson = performance.now() - startJson;
|
|
14
|
+
const startRFDC = performance.now();
|
|
15
|
+
const rfdcClone = clone(obj);
|
|
16
|
+
const durationMsRFDC = performance.now() - startRFDC;
|
|
17
|
+
if (!window.performanceEvents) {
|
|
18
|
+
window.performanceEvents = [];
|
|
19
|
+
}
|
|
20
|
+
const performanceEvent = {
|
|
21
|
+
durationMsJson,
|
|
22
|
+
durationMsRFDC,
|
|
23
|
+
input: obj,
|
|
24
|
+
};
|
|
25
|
+
window.performanceEvents.push(performanceEvent);
|
|
26
|
+
window.performanceSummary = window.performanceSummary || {
|
|
27
|
+
eventForWhichJsonWasMostFaster: null,
|
|
28
|
+
eventForWhichRFDCWasMostFaster: null,
|
|
29
|
+
totalDurationMsJson: 0,
|
|
30
|
+
totalDurationMsRFDC: 0,
|
|
31
|
+
invocationsInWhichJsonWasFaster: [],
|
|
32
|
+
invocationsInWhichRFDCWasFaster: [],
|
|
33
|
+
};
|
|
34
|
+
window.performanceSummary.totalDurationMsJson += durationMsJson;
|
|
35
|
+
window.performanceSummary.totalDurationMsRFDC += durationMsRFDC;
|
|
36
|
+
const isJsonFaster = durationMsJson < durationMsRFDC;
|
|
37
|
+
if (isJsonFaster) {
|
|
38
|
+
window.performanceSummary.invocationsInWhichJsonWasFaster.push(performanceEvent);
|
|
39
|
+
if (!window.performanceSummary.eventForWhichJsonWasMostFaster ||
|
|
40
|
+
durationMsJson < window.performanceSummary.eventForWhichJsonWasMostFaster.durationMsJson) {
|
|
41
|
+
window.performanceSummary.eventForWhichJsonWasMostFaster = performanceEvent;
|
|
12
42
|
}
|
|
13
|
-
|
|
14
|
-
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
window.performanceSummary.invocationsInWhichRFDCWasFaster.push(performanceEvent);
|
|
46
|
+
if (!window.performanceSummary.eventForWhichRFDCWasMostFaster ||
|
|
47
|
+
durationMsRFDC < window.performanceSummary.eventForWhichRFDCWasMostFaster.durationMsRFDC) {
|
|
48
|
+
window.performanceSummary.eventForWhichRFDCWasMostFaster = performanceEvent;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
console.log('cloneDeep performance summary:', window.performanceSummary);
|
|
52
|
+
return rfdcClone;
|
|
15
53
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
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 very large objects:');
|
|
9
|
+
let veryLargeObject = {};
|
|
10
|
+
for (let i = 0; i < 100000; i++) {
|
|
11
|
+
veryLargeObject[`key${i}`] = {
|
|
12
|
+
sub1: {
|
|
13
|
+
val: i,
|
|
14
|
+
},
|
|
15
|
+
sub2: new Date('2023-10-01T12:00:00.000Z'),
|
|
16
|
+
sub3: {
|
|
17
|
+
val: i + 1,
|
|
18
|
+
valX: i + 2,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
console.time('cloneDeep');
|
|
23
|
+
(0, cloneDeep_1.cloneDeep)(veryLargeObject);
|
|
24
|
+
console.timeEnd('cloneDeep');
|
|
25
|
+
console.time('cloneDeepLodash');
|
|
26
|
+
(0, lodash_1.cloneDeep)(veryLargeObject);
|
|
27
|
+
console.timeEnd('cloneDeepLodash');
|
|
28
|
+
console.time('cloneDeepJson');
|
|
29
|
+
jsonClone(veryLargeObject);
|
|
30
|
+
console.timeEnd('cloneDeepJson');
|
|
31
|
+
console.time('structuredClone');
|
|
32
|
+
structuredClone(veryLargeObject);
|
|
33
|
+
console.timeEnd('structuredClone');
|
|
34
|
+
console.log('\n\nPerformance test for very large arrays:');
|
|
35
|
+
let veryLargeArray = [];
|
|
36
|
+
for (let i = 0; i < 100000; i++) {
|
|
37
|
+
veryLargeArray.push({
|
|
38
|
+
sub1: {
|
|
39
|
+
val: i,
|
|
40
|
+
},
|
|
41
|
+
sub2: new Date('2023-10-01T12:00:00.000Z'),
|
|
42
|
+
sub3: {
|
|
43
|
+
val: i + 1,
|
|
44
|
+
valX: i + 2,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
console.time('cloneDeepArray');
|
|
49
|
+
(0, cloneDeep_1.cloneDeep)(veryLargeArray);
|
|
50
|
+
console.timeEnd('cloneDeepArray');
|
|
51
|
+
console.time('cloneDeepArrayLodash');
|
|
52
|
+
(0, lodash_1.cloneDeep)(veryLargeArray);
|
|
53
|
+
console.timeEnd('cloneDeepArrayLodash');
|
|
54
|
+
console.time('cloneDeepArrayJson');
|
|
55
|
+
jsonClone(veryLargeArray);
|
|
56
|
+
console.timeEnd('cloneDeepArrayJson');
|
|
57
|
+
console.time('structuredCloneArray');
|
|
58
|
+
structuredClone(veryLargeArray);
|
|
59
|
+
console.timeEnd('structuredCloneArray');
|
|
60
|
+
console.log('\n\nPerformance test for assortment.json:');
|
|
61
|
+
const fs = require("fs");
|
|
62
|
+
const path = require("path");
|
|
63
|
+
const assortmentFilePath = path.join(__dirname, 'assortment.json');
|
|
64
|
+
const assortmentData = JSON.parse(fs.readFileSync(assortmentFilePath, 'utf8'));
|
|
65
|
+
console.time('cloneDeepAssortment25k');
|
|
66
|
+
(0, cloneDeep_1.cloneDeep)(assortmentData);
|
|
67
|
+
console.timeEnd('cloneDeepAssortment25k');
|
|
68
|
+
console.time('cloneDeepAssortment25kLodash');
|
|
69
|
+
(0, lodash_1.cloneDeep)(assortmentData);
|
|
70
|
+
console.timeEnd('cloneDeepAssortment25kLodash');
|
|
71
|
+
console.time('cloneDeepAssortment25kJson');
|
|
72
|
+
jsonClone(assortmentData);
|
|
73
|
+
console.timeEnd('cloneDeepAssortment25kJson');
|
|
74
|
+
console.time('structuredCloneAssortment25k');
|
|
75
|
+
structuredClone(assortmentData);
|
|
76
|
+
console.timeEnd('structuredCloneAssortment25k');
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare function retainOnlyProperties<T extends Record<string, any>, K extends readonly string[]>(obj: T[], keysToKeep: K, options?: {
|
|
2
|
-
shouldDeleteInPlace
|
|
2
|
+
shouldDeleteInPlace?: boolean;
|
|
3
|
+
skipKeys?: readonly string[];
|
|
3
4
|
}): Array<Pick<T, K[number]>>;
|
|
4
5
|
export declare function retainOnlyProperties<T extends Record<string, any>, K extends readonly string[]>(obj: T, keysToKeep: K, options?: {
|
|
5
|
-
shouldDeleteInPlace
|
|
6
|
+
shouldDeleteInPlace?: boolean;
|
|
7
|
+
skipKeys?: readonly string[];
|
|
6
8
|
}): Pick<T, K[number]>;
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.retainOnlyProperties = retainOnlyProperties;
|
|
4
4
|
const object_util_1 = require("../object-util");
|
|
5
|
-
function retainOnlyProperties(obj, keysToKeep, options = {
|
|
5
|
+
function retainOnlyProperties(obj, keysToKeep, options = {}) {
|
|
6
6
|
const keepSet = new Set(keysToKeep);
|
|
7
|
+
const skipSet = new Set(options.skipKeys || []);
|
|
7
8
|
const shouldReturnAsIs = (value) => value === null || value === undefined || typeof value !== 'object' || value instanceof Date;
|
|
8
9
|
const retain = (target) => {
|
|
9
10
|
if (shouldReturnAsIs(target))
|
|
@@ -12,6 +13,9 @@ function retainOnlyProperties(obj, keysToKeep, options = { shouldDeleteInPlace:
|
|
|
12
13
|
return target.map(retain);
|
|
13
14
|
}
|
|
14
15
|
for (const key in target) {
|
|
16
|
+
if (skipSet.has(key)) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
15
19
|
if (!keepSet.has(key)) {
|
|
16
20
|
delete target[key];
|
|
17
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/util",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.14-alpha-1",
|
|
4
4
|
"description": "General javascript utilities",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@contrail/types": "^3.0.95",
|
|
43
43
|
"fflate": "^0.8.2",
|
|
44
|
-
"lodash": "^4.17.21"
|
|
44
|
+
"lodash": "^4.17.21",
|
|
45
|
+
"rfdc": "^1.4.1"
|
|
45
46
|
}
|
|
46
47
|
}
|