From 088ee3c1ea0466521948c3911da0db8517ea5b97 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 23 Dec 2022 18:25:30 +0100 Subject: [PATCH] Switch to more efficient object deep cloning and shallow merging implementations MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package-lock.json | 36 +++++++++++++++++++------ package.json | 2 ++ rollup.config.mjs | 2 ++ src/charging-station/ChargingStation.ts | 4 +-- src/utils/Configuration.ts | 32 +++++----------------- src/utils/Utils.ts | 6 +++-- 6 files changed, 45 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 55bad821..2f1ebea9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,8 @@ "basic-ftp": "^5.0.2", "chalk": "^4.1.2", "http-status-codes": "^2.2.0", + "just-clone": "^6.2.0", + "just-merge": "^3.2.0", "mnemonist": "^0.39.5", "moment": "^2.29.4", "mongodb": "^4.13.0", @@ -4278,12 +4280,11 @@ } }, "node_modules/cacheable-request": { - "version": "10.2.3", - "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.3.tgz", - "integrity": "sha512-6BehRBOs7iurNjAYN9iPazTwFDaMQavJO8W1MEm3s2pH8q/tkPTtLDRUZaweWK87WFGf2Y5wLAlaCJlR5kOz3w==", + "version": "10.2.4", + "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.4.tgz", + "integrity": "sha512-IWIea8ei1Ht4dBqvlvh7Gs7EYlMyBhlJybLDUB9sadEqHqftmdNieMLIR5ia3vs8gbjj9t8hXLBpUVg3vcQNbg==", "dev": true, "dependencies": { - "@types/http-cache-semantics": "^4.0.1", "get-stream": "^6.0.1", "http-cache-semantics": "^4.1.0", "keyv": "^4.5.2", @@ -9553,6 +9554,16 @@ "node": ">=0.6.0" } }, + "node_modules/just-clone": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/just-clone/-/just-clone-6.2.0.tgz", + "integrity": "sha512-1IynUYEc/HAwxhi3WDpIpxJbZpMCvvrrmZVqvj9EhpvbH8lls7HhdhiByjL7DkAaWlLIzpC0Xc/VPvy/UxLNjA==" + }, + "node_modules/just-merge": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/just-merge/-/just-merge-3.2.0.tgz", + "integrity": "sha512-cNh5FWt44hx4SpQS1xZU8Tzr/fQA69pqCdjbwxmaYYIOuRfA8EIg+dn1bGmIW03ZUtR2vkMOCjWKc+jIbpauSw==" + }, "node_modules/keyv": { "version": "4.5.2", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.2.tgz", @@ -20786,12 +20797,11 @@ "dev": true }, "cacheable-request": { - "version": "10.2.3", - "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.3.tgz", - "integrity": "sha512-6BehRBOs7iurNjAYN9iPazTwFDaMQavJO8W1MEm3s2pH8q/tkPTtLDRUZaweWK87WFGf2Y5wLAlaCJlR5kOz3w==", + "version": "10.2.4", + "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.4.tgz", + "integrity": "sha512-IWIea8ei1Ht4dBqvlvh7Gs7EYlMyBhlJybLDUB9sadEqHqftmdNieMLIR5ia3vs8gbjj9t8hXLBpUVg3vcQNbg==", "dev": true, "requires": { - "@types/http-cache-semantics": "^4.0.1", "get-stream": "^6.0.1", "http-cache-semantics": "^4.1.0", "keyv": "^4.5.2", @@ -24957,6 +24967,16 @@ "verror": "1.10.0" } }, + "just-clone": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/just-clone/-/just-clone-6.2.0.tgz", + "integrity": "sha512-1IynUYEc/HAwxhi3WDpIpxJbZpMCvvrrmZVqvj9EhpvbH8lls7HhdhiByjL7DkAaWlLIzpC0Xc/VPvy/UxLNjA==" + }, + "just-merge": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/just-merge/-/just-merge-3.2.0.tgz", + "integrity": "sha512-cNh5FWt44hx4SpQS1xZU8Tzr/fQA69pqCdjbwxmaYYIOuRfA8EIg+dn1bGmIW03ZUtR2vkMOCjWKc+jIbpauSw==" + }, "keyv": { "version": "4.5.2", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.2.tgz", diff --git a/package.json b/package.json index ea5c1889..43204820 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,8 @@ "basic-ftp": "^5.0.2", "chalk": "^4.1.2", "http-status-codes": "^2.2.0", + "just-clone": "^6.2.0", + "just-merge": "^3.2.0", "mnemonist": "^0.39.5", "moment": "^2.29.4", "mongodb": "^4.13.0", diff --git a/rollup.config.mjs b/rollup.config.mjs index 4378f014..231b7485 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -44,6 +44,8 @@ export default { 'fs', 'http', 'http-status-codes', + 'just-clone', + 'just-merge', 'mnemonist/lru-map-with-delete', 'moment', 'mongodb', diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 5c8ec1e4..f7cad5db 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1890,9 +1890,9 @@ export default class ChargingStation { } private getConfiguredSupervisionUrl(): URL { - const supervisionUrls = Utils.cloneObject( + const supervisionUrls = ( this.stationInfo.supervisionUrls ?? Configuration.getSupervisionUrls() - ); + ).slice(); if (!Utils.isEmptyArray(supervisionUrls)) { switch (Configuration.getSupervisionUrlDistribution()) { case SupervisionUrlDistribution.ROUND_ROBIN: diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index c17cad01..3770c6ca 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -3,6 +3,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import chalk from 'chalk'; +import merge from 'just-merge'; import { WorkerChoiceStrategies } from 'poolifier'; import { @@ -68,7 +69,7 @@ export default class Configuration { }, }; if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) { - uiServerConfiguration = Configuration.deepMerge( + uiServerConfiguration = Configuration.merge( uiServerConfiguration, Configuration.getConfig().uiServer ); @@ -409,9 +410,9 @@ export default class Configuration { } } - private static isObject(item: unknown): boolean { - return item && typeof item === 'object' && Array.isArray(item) === false; - } + // private static isObject(item: unknown): boolean { + // return item && typeof item === 'object' && Array.isArray(item) === false; + // } private static objectHasOwnProperty(object: unknown, property: string): boolean { return Object.prototype.hasOwnProperty.call(object, property) as boolean; @@ -421,27 +422,8 @@ export default class Configuration { return typeof obj === 'undefined'; } - private static deepMerge(target: object, ...sources: object[]): object { - if (!sources.length) { - return target; - } - const source = sources.shift(); - - if (Configuration.isObject(target) && Configuration.isObject(source)) { - for (const key in source) { - if (Configuration.isObject(source[key])) { - if (!target[key]) { - Object.assign(target, { [key]: {} }); - } - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - Configuration.deepMerge(target[key], source[key]); - } else { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - Object.assign(target, { [key]: source[key] }); - } - } - } - return Configuration.deepMerge(target, ...sources); + private static merge(target: object, ...sources: object[]): object { + return merge(target, ...sources); } private static handleFileException( diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index f27f0666..2fde4883 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,5 +1,7 @@ import crypto from 'crypto'; +import clone from 'just-clone'; + import { WebSocketCloseEventStatusString } from '../types/WebSocket'; export default class Utils { @@ -163,8 +165,8 @@ export default class Utils { ); } - public static cloneObject(object: T): T { - return JSON.parse(JSON.stringify(object)) as T; + public static cloneObject(object: T): T { + return clone(object); } public static isIterable(obj: T): boolean { -- 2.34.1