Open
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces caching of global variables in the sandbox to reduce proxy overhead and improve script execution performance.
- Added
constant.tsto define and filter a list of globals for caching. - Prepended a destructuring statement in
index.tsto cache these globals before executing the sandboxed script.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/sandbox/src/index.ts | Prepends destructured global variable declarations (cachedGlobals) before script execution. |
| packages/sandbox/src/constant.ts | Defines globalsInES2015 and composes the cachedGlobals array of properties to cache. |
Comments suppressed due to low confidence (3)
packages/sandbox/src/index.ts:196
- [nitpick] The name
scopedGlobalVariableDefinitionis singular but contains multiple declarations. Consider renaming toscopedGlobalVariableDeclarationsfor clarity.
const scopedGlobalVariableDefinition = cachedGlobals.length ? `const {${cachedGlobals.join(',')}}=sandbox;` : '';
packages/sandbox/src/index.ts:195
- [nitpick] Add a unit test to verify that sandbox execution with cached globals yields the same behavior as without caching, ensuring functional correctness.
// Concatenate scopedGlobalVariables into variable declarations to cache global variables, avoiding proxy traversal on every use.
packages/sandbox/src/constant.ts:55
- Destructuring
undefinedas an identifier causes a syntax error sinceundefinedis a reserved word. Remove it fromcachedGlobalsor cache it via a separate assignment (e.g.,const undefinedVar = sandbox.undefined;).
'undefined',
Author
|
补充:已通过代码中全部sandbox测试用例 |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
在使用icestarkk开启沙箱接入子应用的时,我们发现在遇到执行的js代码需要频繁访问沙箱时,会遇到明显的性能问题和卡顿。
针对此问题,笔者想到了一种解决方法:
在使用时将常用的变量进行缓存,这样相关的变量就只会访问一次沙箱,之后代码执行时就不再需要再多次重复访问沙箱。
在后续学习中发现另一个微前端框架qiankun也在沙箱中默认开启使用类似的缓存方法来对沙箱性能进行优化,pr中的缓存的变量以及写法参考了qiankun。
下面是一个将icestark的sandbox核心代码简化后的例子:
未进行缓存时候,以上代码中的Math等变量每执行一次都会访问沙箱造成不必要的性能开销,以上代码实际执行下来的时间大约在70ms。
如果简易沙箱开启缓存后,代码实际执行时间在0.8ms,性能差距非常明显。(图片在最后)
下面是简易沙箱开启缓存的示例代码:
在icestark沙箱中加上缓存后实际测试的效果:
测试示例代码未进行缓存前平均执行时间在54ms左右,开启缓存后的平均执行时间在19ms作用,速度提升了三倍左右
测试代码:
简易沙箱示例执行结果图片:

