Skip to content

Commit

Permalink
Improve perf by copying natives function into context
Browse files Browse the repository at this point in the history
  • Loading branch information
Karang committed May 11, 2020
1 parent 14167fa commit 7897c47
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ReadCompiler {
throw new Error('Unknown parametrizable type: ' + type[0])
} else { // Primitive type
if (type === 'native') return 'null'
if (this.types[type] && this.types[type] !== 'native') { return 'ctx.' + type }
if (this.types[type]) { return 'ctx.' + type }
return this.primitiveTypes[type]
}
}
Expand All @@ -207,12 +207,16 @@ class ReadCompiler {
let functions = []
this.types = types
for (const type in this.context) {
functions.push(`${type}: ` + this.context[type])
functions[type] = this.context[type]
}
for (const type in types) {
if (types[type] !== 'native') { functions.push(`${type}: ` + this.compileType(types[type])) }
if (!functions[type]) {
if (types[type] !== 'native') { functions[type] = this.compileType(types[type]) } else { functions[type] = `native.${type}` }
}
}
return '() => {\nconst ctx = {\n' + indent(functions.join(',\n')) + '\n}\n return ctx\n}'
return '() => {\nconst ctx = {\n' + indent(Object.keys(functions).map((type) => {
return type + ': ' + functions[type]
}).join(',\n')) + '\n}\n return ctx\n}'
}

/**
Expand Down

0 comments on commit 7897c47

Please sign in to comment.