⚡️ avoid duplicate value check to improve performance (#1134)
This commit is contained in:
parent
fbbd6f1722
commit
a023a5c9b7
|
|
@ -16,17 +16,18 @@ export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) {
|
||||||
|
|
||||||
const functionBoundedValueMap = new WeakMap<Function, Function>();
|
const functionBoundedValueMap = new WeakMap<Function, Function>();
|
||||||
export function getTargetValue(target: any, value: any): any {
|
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 中是否还有可枚举的拓展方法的方式来判断
|
仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类。目前没有完美的检测方式,这里通过 prototype 中是否还有可枚举的拓展方法的方式来判断
|
||||||
@warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常)
|
@warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常)
|
||||||
*/
|
*/
|
||||||
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
|
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
|
||||||
const cachedBoundValue = functionBoundedValueMap.get(value);
|
|
||||||
if (cachedBoundValue) {
|
|
||||||
return cachedBoundValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const boundValue = Function.prototype.bind.call(value, target);
|
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.
|
// 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
|
// use for..in rather than Object.keys.forEach for performance reason
|
||||||
// eslint-disable-next-line guard-for-in,no-restricted-syntax
|
// eslint-disable-next-line guard-for-in,no-restricted-syntax
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user