From a023a5c9b75a4025122d1a3b85c2031b81e791ec Mon Sep 17 00:00:00 2001 From: Kuitos Date: Tue, 8 Dec 2020 14:40:10 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20avoid=20duplicate=20value?= =?UTF-8?q?=20check=20to=20improve=20performance=20(#1134)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/common.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/sandbox/common.ts b/src/sandbox/common.ts index 65475d5..0e21915 100644 --- a/src/sandbox/common.ts +++ b/src/sandbox/common.ts @@ -16,17 +16,18 @@ export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) { const functionBoundedValueMap = new WeakMap(); export function getTargetValue(target: any, value: any): any { + const cachedBoundFunction = functionBoundedValueMap.get(value); + if (cachedBoundFunction) { + return cachedBoundFunction; + } + /* 仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.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