@@ -5,20 +5,26 @@ module.exports = {
5
5
compileExpression
6
6
}
7
7
8
+ const expressionCache = new Map ( )
9
+ const codeCache = new Map ( )
10
+
8
11
function compileExpression ( src , sandbox ) {
9
12
if ( typeof src !== 'string' ) {
10
13
throw new TypeError ( 'first argument must be a string' )
11
14
}
12
15
if ( typeof sandbox !== 'object' ) {
13
16
throw new TypeError ( 'second argument must be an object' )
14
17
}
15
-
16
- sandbox = new Proxy ( sandbox , { get, has} )
17
- const expression = `
18
- try { with (sandbox) { return ${ src } } } catch (err) {
19
- if (!(err instanceof ReferenceError || err instanceof TypeError)) throw err
20
- }`
21
- return new Function ( 'sandbox' , expression ) . bind ( sandbox , sandbox ) // eslint-disable-line
18
+ sandbox = new Proxy ( sandbox , { has} )
19
+ let expression = expressionCache . get ( src )
20
+ if ( ! expression ) {
21
+ expression = new Function ( 'sandbox' ,
22
+ `try { with (sandbox) { return ${ src } } } catch (err) {
23
+ if (!(err instanceof ReferenceError || err instanceof TypeError)) throw err
24
+ }` )
25
+ expressionCache . set ( src , expression )
26
+ }
27
+ return expression . bind ( sandbox , sandbox ) // eslint-disable-line
22
28
}
23
29
24
30
function compileCode ( src , sandbox ) {
@@ -28,18 +34,15 @@ function compileCode (src, sandbox) {
28
34
if ( typeof sandbox !== 'object' ) {
29
35
throw new TypeError ( 'second argument must be an object' )
30
36
}
31
-
32
- sandbox = new Proxy ( sandbox , { get, has} )
33
- return new Function ( 'sandbox' , `with (sandbox) { ${ src } }` ) . bind ( sandbox , sandbox ) // eslint-disable-line
34
- }
35
-
36
- function get ( target , key , receiver ) {
37
- if ( key === Symbol . unscopables ) {
38
- return undefined
37
+ sandbox = new Proxy ( sandbox , { has} )
38
+ let code = codeCache . get ( src )
39
+ if ( ! code ) {
40
+ code = new Function ( 'sandbox' , `with (sandbox) { ${ src } }` )
41
+ codeCache . set ( src , code )
39
42
}
40
- return Reflect . get ( target , key , receiver )
43
+ return code . bind ( sandbox , sandbox ) // eslint-disable-line
41
44
}
42
45
43
- function has ( target , key ) {
46
+ function has ( ) {
44
47
return true
45
48
}
0 commit comments