Skip to content

Commit 625acaf

Browse files
committed
perf(rawExpression): improve performance for simple expressions
1 parent 1542983 commit 625acaf

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/rawCompiler.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1+
const isSimple = /^(\w|\.)+$/
2+
13
export function compileRawExpression (src) {
4+
return isSimple.test(src) ? compileSimpleExpression(src) : compileComplexExpression(src)
5+
}
6+
7+
function compileSimpleExpression (src) {
8+
return new Function('context', 'tempVars', // eslint-disable-line
9+
`if (tempVars) {
10+
try { return tempVars.${src} } catch (err) {
11+
if (!(err instanceof TypeError)) throw err
12+
}
13+
}
14+
try { return context.${src} } catch (err) {
15+
if (!(err instanceof TypeError)) throw err
16+
}`)
17+
}
18+
19+
function compileComplexExpression (src) {
220
return new Function('context', 'tempVars', // eslint-disable-line
321
`const sandbox = $nxCompileToSandbox(context, tempVars)
422
try { with (sandbox) { return ${src} } } catch (err) {

0 commit comments

Comments
 (0)