️ avoid duplicate value check to improve performance (#1134)

This commit is contained in:
Kuitos 2020-12-08 14:40:10 +08:00 committed by GitHub
parent fbbd6f1722
commit a023a5c9b7

View File

@ -16,17 +16,18 @@ export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) {
const functionBoundedValueMap = new WeakMap<Function, Function>();
export function getTargetValue(target: any, value: any): any {
const cachedBoundFunction = functionBoundedValueMap.get(value);
if (cachedBoundFunction) {
return cachedBoundFunction;
}
/*
isCallable && !isBoundedFunction && !isConstructable window.consolewindow.atob prototype
@warning edge case lodash.isFunction iframe top window
*/
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
const cachedBoundValue = functionBoundedValueMap.get(value);
if (cachedBoundValue) {
return cachedBoundValue;
}
const boundValue = Function.prototype.bind.call(value, target);
// some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function.
// use for..in rather than Object.keys.forEach for performance reason
// eslint-disable-next-line guard-for-in,no-restricted-syntax