Skip to content

Commit e52d1ca

Browse files
committed
perf: release memory
1 parent 6edc13a commit e52d1ca

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

src/array.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function arrayEach<V>(
2626
else if (re === true) continue;
2727
}
2828
}
29+
// @ts-ignore
30+
array = null;
2931
}
3032

3133
/**

src/object.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function objectEach<O extends AnyObject>(
3636
if (!objectHas(obj, key)) continue;
3737
if (iterator(obj[key], key) === false) break;
3838
}
39+
// @ts-ignore
40+
obj = null;
3941
}
4042

4143
/**
@@ -69,7 +71,8 @@ export function objectMap<O extends AnyObject, T>(
6971
if (!objectHas(obj, key)) continue;
7072
obj2[key] = iterator(obj[key], key);
7173
}
72-
74+
// @ts-ignore
75+
obj = null;
7376
return obj2;
7477
}
7578

@@ -90,6 +93,8 @@ export function objectPick<O extends AnyObject, K extends Extract<keyof O, strin
9093
obj2[k] = v;
9194
}
9295
});
96+
// @ts-ignore
97+
obj = null;
9398
return obj2;
9499
}
95100
/**
@@ -109,6 +114,8 @@ export function objectOmit<O extends AnyObject, K extends Extract<keyof O, strin
109114
obj2[k] = v;
110115
}
111116
});
117+
// @ts-ignore
118+
obj = null;
112119
return obj2;
113120
}
114121

@@ -205,14 +212,14 @@ export function objectFill<R extends AnyObject = AnyObject>(
205212
return source as R;
206213
}
207214
/**
208-
* 获取对象指定层级下的属性值(现在可用ES6+的可选链?.来替代)
215+
* 获取对象/数组指定层级下的属性值(现在可用ES6+的可选链?.来替代)
209216
* @param {AnyObject} obj
210217
* @param {string} path
211218
* @param {boolean} strict
212219
* @returns
213220
*/
214221
export function objectGet(
215-
obj: AnyObject,
222+
obj: AnyObject | AnyArray | undefined,
216223
path: string,
217224
strict = false
218225
): {

src/tree.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ export function forEachDeep<V>(
8888
}
8989
};
9090
walk(tree, null);
91+
// @ts-ignore
92+
tree = null;
9193
}
9294

9395
/**
@@ -165,6 +167,9 @@ export function mapDeep<T>(
165167
}
166168
};
167169
walk(tree, null, newTree);
170+
171+
// @ts-ignore
172+
tree = null;
168173
return newTree;
169174
}
170175
export type IdLike = number | string;
@@ -229,7 +234,8 @@ export function formatTree(list: any[], options: IFieldOptions = defaultFieldOpt
229234
treeArr.push(item);
230235
}
231236
});
232-
237+
// @ts-ignore
238+
list = null;
233239
return treeArr;
234240
}
235241

test/dom.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,4 @@ test('getComputedCssVal', () => {
6363
});
6464
test('getStrWidthPx', () => {
6565
const width = getStrWidthPx('hello javascript');
66-
console.log('width', width);
6766
});

test/object.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,7 @@ test('objectGet', () => {
378378
expect(objectGet(object2, 'a[1].b.c').p).toBeUndefined();
379379
expect(objectGet(object2, 'a[1].b.c').k).toBeUndefined();
380380
expect(objectGet(object2, 'a[1].b.c').v).toBeUndefined();
381+
382+
expect(objectGet([11, { aa: 1 }], '[0]').v).toBe(11);
383+
expect(objectGet(undefined, '[0]').v).toBeUndefined();
381384
});

test/tree.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ const treeArray = generateTreeArray(1000);
225225
test('compare formatTree buildTree', () => {
226226
// console.log('arr', arr);
227227
const arr = cloneDeep(treeArray) as any[];
228-
// const startTime = Date.now();
228+
console.time('buildTree time');
229229
const tree1 = buildTree('id', 'pid', arr);
230-
// console.log('buildTree time:', Date.now() - startTime);
230+
console.timeEnd('buildTree time');
231231

232-
// const startTime2 = Date.now();
232+
console.time('formatTree time');
233233
const tree2 = formatTree(arr, { keyField: 'id', childField: 'children', pidField: 'pid' });
234-
// console.log('formatTree time:', Date.now() - startTime2);
234+
console.timeEnd('formatTree time');
235235

236236
expect(tree1).toEqual(tree2);
237237
});

0 commit comments

Comments
 (0)