From ee2f5eb24c6ea55e0f443124663ee7385c2ea772 Mon Sep 17 00:00:00 2001 From: piman <44853490+piman51277@users.noreply.github.com> Date: Sat, 8 Jul 2023 21:03:46 -0400 Subject: [PATCH 1/3] Optimize getTexture func Minor optimization updates --- src/shared/getTexture.ts | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/shared/getTexture.ts b/src/shared/getTexture.ts index ee6cd23..3dd6f25 100644 --- a/src/shared/getTexture.ts +++ b/src/shared/getTexture.ts @@ -2,9 +2,7 @@ import isNumber from '../util/isNumber'; import { ISchema } from '../types/schema'; -const TEXTURE_EXCEPTIONS = [ - ['Health and Hell', 'Health and Hell (Green)'], -]; +const TEXTURE_EXCEPTIONS = [['Health and Hell', 'Health and Hell (Green)']]; /** * Iterates over effects object to get matching effect. @@ -16,25 +14,23 @@ export default function ( ): string | void { const textures = attributes.schema.getTextures(); const textureKeys = Object.keys(textures); + + for (let j = 0; j < TEXTURE_EXCEPTIONS.length; j++) { + const exception = TEXTURE_EXCEPTIONS[j]; + if (name.includes(`${exception[1]} `)) return exception[1]; + } + + const skipHauntedFlag = name.includes('Haunted Ghosts') && !attributes.wear; + for (let i = 0; i < textureKeys.length; i++) { - const texture: number | string = textureKeys[i]; + const texture = textureKeys[i]; - if ( - texture === 'Haunted Ghosts' && - name.includes('Haunted Ghosts') && - !attributes.wear - ) { + if (!name.includes(`${texture} `) || isNumber(texture)) { + // eslint-disable-next-line no-continue continue; } - for (let j = 0; j < TEXTURE_EXCEPTIONS.length; j++) { - const exception = TEXTURE_EXCEPTIONS[j]; - if (texture === exception[0] && name.includes(`${exception[1]} `)) - return exception[1]; - } - - if (isNumber(texture) || !name.includes(`${texture} `)) { - // eslint-disable-next-line no-continue + if (texture === 'Haunted Ghosts' && skipHauntedFlag) { continue; } From 13bac0c088638762900ab60899edb8bf44e94485 Mon Sep 17 00:00:00 2001 From: piman <44853490+piman51277@users.noreply.github.com> Date: Sat, 8 Jul 2023 21:15:03 -0400 Subject: [PATCH 2/3] Add back missing eslint exception --- src/shared/getTexture.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/shared/getTexture.ts b/src/shared/getTexture.ts index 3dd6f25..5f970cd 100644 --- a/src/shared/getTexture.ts +++ b/src/shared/getTexture.ts @@ -31,6 +31,7 @@ export default function ( } if (texture === 'Haunted Ghosts' && skipHauntedFlag) { + // eslint-disable-next-line no-continue continue; } From 81c402c034852104f2eb926b85899c9e407e03d7 Mon Sep 17 00:00:00 2001 From: piman <44853490+piman51277@users.noreply.github.com> Date: Sat, 8 Jul 2023 23:06:39 -0400 Subject: [PATCH 3/3] Further optimize getTexture + Caches result of Object.keys() for increased performance + Strips unused items from search pool and caches the result Now provides ~40% boost for static and a ~100% boost for more optimized implementations. (For the parseEconItem function) --- src/shared/getTexture.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/shared/getTexture.ts b/src/shared/getTexture.ts index 5f970cd..45278c0 100644 --- a/src/shared/getTexture.ts +++ b/src/shared/getTexture.ts @@ -1,9 +1,18 @@ import isNumber from '../util/isNumber'; import { ISchema } from '../types/schema'; +import { SchemaEnum } from 'tf2-static-schema'; const TEXTURE_EXCEPTIONS = [['Health and Hell', 'Health and Hell (Green)']]; +//cache the textureKeys since Object.keys() is expensive +let isTextureKeysCached = false; +let textureKeys: string[] = []; + +//cache a stripped version of textures +let isTexturesCached = false; +let textures: SchemaEnum = {}; + /** * Iterates over effects object to get matching effect. */ @@ -12,8 +21,24 @@ export default function ( name: string, attributes: { wear: any | null; schema: ISchema } ): string | void { - const textures = attributes.schema.getTextures(); - const textureKeys = Object.keys(textures); + if (!isTexturesCached) { + const copysrc = attributes.schema.getTextures(); + + //strip away all properties with numeric keys + //we must also copy this object, as we don't want to modify the original + const keys = Object.keys(copysrc); + for (const key of keys) { + if (!isNumber(key)) { + textures[key] = copysrc[key]; + } + } + + isTexturesCached = true; + } + if (!isTextureKeysCached) { + textureKeys = Object.keys(textures); + isTextureKeysCached = true; + } for (let j = 0; j < TEXTURE_EXCEPTIONS.length; j++) { const exception = TEXTURE_EXCEPTIONS[j]; @@ -25,7 +50,7 @@ export default function ( for (let i = 0; i < textureKeys.length; i++) { const texture = textureKeys[i]; - if (!name.includes(`${texture} `) || isNumber(texture)) { + if (!name.includes(`${texture} `)) { // eslint-disable-next-line no-continue continue; }