Просмотр исходного кода

perf(wise-cost-util): 优化getIndex速度

vian 3 лет назад
Родитель
Сommit
9fd059660a
1 измененных файлов с 6 добавлено и 5 удалено
  1. 6 5
      wise-cost-util/src/glj.ts

+ 6 - 5
wise-cost-util/src/glj.ts

@@ -38,14 +38,15 @@ export const calcMarketPriceByInfoPrice = (infoPrice: number, purchaseFeeRate: n
 
 // roundForObj()
 // 返回五大项组成的索引
-export const getIndex = (obj: IBaseGlj, pops = ['code', 'name', 'specs', 'unit', 'type']): string => {
+export const getIndex = (obj: IBaseGlj, props = ['code', 'name', 'specs', 'unit', 'type']): string => {
   let index = '';
-  const arr = [];
-  for (const p of pops) {
+  //! 优化:字符串拼接比数组push、join操作快很多,大项目下影响较大,所以使用字符串拼接
+  for (let i = 0; i < props.length; i += 1) {
+    const p = props[i];
     const tmpK = obj[p] === undefined || obj[p] === null || obj[p] === '' ? 'null' : obj[p];
-    arr.push(tmpK);
+    const str = i === props.length - 1 ? tmpK : `${tmpK}|-|`;
+    index += str;
   }
-  index = arr.join('|-|');
   return index;
 };