From b4c82e730be585cb7d7005d66b781d4a02c18a85 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 13 Feb 2024 23:24:21 +0100 Subject: [PATCH] perf: switch from lodash to rambda MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .vscode/settings.json | 1 + bundle.js | 2 +- package.json | 3 +- pnpm-lock.yaml | 24 ++++-------- src/charging-station/ChargingStation.ts | 6 +-- src/utils/Configuration.ts | 4 +- src/utils/Utils.ts | 50 +------------------------ tests/utils/Utils.test.ts | 3 -- 8 files changed, 18 insertions(+), 75 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index af16af81..1138aad3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -43,6 +43,7 @@ "piment", "poolifier", "preinstall", + "rambda", "Recurrency", "RFID", "shutdowning", diff --git a/bundle.js b/bundle.js index 698f52a8..912d16d9 100644 --- a/bundle.js +++ b/bundle.js @@ -23,12 +23,12 @@ await build({ 'chalk', 'date-fns', 'http-status-codes', - 'lodash-es', 'logform', 'mnemonist', 'mongodb', 'node:*', 'poolifier', + 'rambda', 'tar', 'winston', 'winston/*', diff --git a/package.json b/package.json index 78c0324f..06e68b49 100644 --- a/package.json +++ b/package.json @@ -102,11 +102,11 @@ "chalk": "^5.3.0", "date-fns": "^3.3.1", "http-status-codes": "^2.3.0", - "lodash-es": "^4.17.21", "logform": "^2.6.0", "mnemonist": "0.40.0-rc1", "mongodb": "^6.3.0", "poolifier": "^3.1.20", + "rambda": "^9.1.0", "tar": "^6.2.0", "winston": "^3.11.0", "winston-daily-rotate-file": "^5.0.0", @@ -121,7 +121,6 @@ "@commitlint/config-conventional": "^18.6.0", "@mikro-orm/cli": "^6.1.3", "@release-it/bumper": "^6.0.1", - "@types/lodash-es": "^4.17.12", "@types/node": "^20.11.17", "@types/tar": "^6.1.11", "@types/ws": "^8.5.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a44402f..3ef7bf3c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -44,9 +44,6 @@ dependencies: http-status-codes: specifier: ^2.3.0 version: 2.3.0 - lodash-es: - specifier: ^4.17.21 - version: 4.17.21 logform: specifier: ^2.6.0 version: 2.6.0 @@ -59,6 +56,9 @@ dependencies: poolifier: specifier: ^3.1.20 version: 3.1.20 + rambda: + specifier: ^9.1.0 + version: 9.1.0 tar: specifier: ^6.2.0 version: 6.2.0 @@ -93,9 +93,6 @@ devDependencies: '@release-it/bumper': specifier: ^6.0.1 version: 6.0.1(release-it@17.0.3) - '@types/lodash-es': - specifier: ^4.17.12 - version: 4.17.12 '@types/node': specifier: ^20.11.17 version: 20.11.17 @@ -1594,16 +1591,6 @@ packages: resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==} dev: true - /@types/lodash-es@4.17.12: - resolution: {integrity: sha512-0NgftHUcV4v34VhXm8QBSftKVXtbkBG3ViCjs6+eJ5a6y6Mi/jiFGPc1sC7QK+9BFhWrURE3EOggmWaSxL9OzQ==} - dependencies: - '@types/lodash': 4.14.202 - dev: true - - /@types/lodash@4.14.202: - resolution: {integrity: sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==} - dev: true - /@types/long@4.0.2: resolution: {integrity: sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==} dev: true @@ -6380,6 +6367,7 @@ packages: /lodash-es@4.17.21: resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} + dev: true /lodash.camelcase@4.3.0: resolution: {integrity: sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==} @@ -8060,6 +8048,10 @@ packages: through2: 2.0.5 dev: true + /rambda@9.1.0: + resolution: {integrity: sha512-fCRAq8Of+bAuqjAA0MSb/umbNgiYwy9N5camM2T++Qu/mRImLT3a31hf0REvnqaWCrgrdC3ewf/ucAILsYeBgQ==} + dev: false + /randombytes@2.1.0: resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} dependencies: diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index a3b0328d..11098648 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -8,7 +8,7 @@ import { URL } from 'node:url' import { parentPort } from 'node:worker_threads' import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns' -import { merge } from 'lodash-es' +import { mergeDeepRight } from 'rambda' import { type RawData, WebSocket } from 'ws' import { AutomaticTransactionGenerator } from './AutomaticTransactionGenerator.js' @@ -1198,7 +1198,7 @@ export class ChargingStation extends EventEmitter { } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'` ) } - stationInfo.firmwareUpgrade = merge( + stationInfo.firmwareUpgrade = mergeDeepRight( { versionUpgrade: { step: 1 @@ -1730,7 +1730,7 @@ export class ChargingStation extends EventEmitter { } else { delete configurationData.configurationKey } - configurationData = merge( + configurationData = mergeDeepRight( configurationData, buildChargingStationAutomaticTransactionGeneratorConfiguration(this) ) diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index de3a79bb..30ae873e 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -4,7 +4,7 @@ import { env } from 'node:process' import { fileURLToPath } from 'node:url' import chalk from 'chalk' -import { merge } from 'lodash-es' +import { mergeDeepRight } from 'rambda' import { buildPerformanceUriFilePath, @@ -161,7 +161,7 @@ export class Configuration { } } if (hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.uiServer)) { - uiServerConfiguration = merge( + uiServerConfiguration = mergeDeepRight( uiServerConfiguration, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion Configuration.getConfigurationData()!.uiServer! diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index cff63715..c0ee87f0 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -12,6 +12,7 @@ import { minutesToSeconds, secondsToMilliseconds } from 'date-fns' +import { clone as cloneDeep } from 'rambda' import { Constants } from './Constants.js' import { @@ -211,55 +212,8 @@ export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] return timeSeries.map(timeSeriesItem => timeSeriesItem.value) } -type CloneableData = - | number - | string - | boolean - | null - | undefined - | Date - | CloneableData[] - | { [key: string]: CloneableData } - -type FormatKey = (key: string) => string - -const deepClone = ( - value: I, - formatKey?: FormatKey, - refs: Map = new Map() -): O => { - const ref = refs.get(value) - if (ref !== undefined) { - return ref - } - if (Array.isArray(value)) { - const clone: CloneableData[] = [] - refs.set(value, clone as O) - for (let i = 0; i < value.length; i++) { - clone[i] = deepClone(value[i], formatKey, refs) - } - return clone as O - } - if (value instanceof Date) { - return new Date(value.getTime()) as O - } - if (typeof value !== 'object' || value === null) { - return value as unknown as O - } - const clone: Record = {} - refs.set(value, clone as O) - for (const key of Object.keys(value)) { - clone[typeof formatKey === 'function' ? formatKey(key) : key] = deepClone( - value[key], - formatKey, - refs - ) - } - return clone as O -} - export const clone = (object: T): T => { - return deepClone(object as CloneableData) as T + return cloneDeep(object) } /** diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 6db4e364..7bee08ac 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -345,9 +345,6 @@ await describe('Utils test suite', async () => { expect(clone(map)).toStrictEqual({}) const set = new Set(['1']) expect(clone(set)).toStrictEqual({}) - // The URL object seems to have not enumerable properties - const url = new URL('https://domain.tld') - expect(clone(url)).toStrictEqual({}) const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]) expect(clone(weakMap)).toStrictEqual({}) const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]) -- 2.34.1