From 7897c47c4af1b5a5c77a122e575c66ebd277ad12 Mon Sep 17 00:00:00 2001 From: Karang Date: Mon, 11 May 2020 22:51:00 +0200 Subject: [PATCH] Improve perf by copying natives function into context --- src/compiler.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/compiler.js b/src/compiler.js index f200fde..848cc92 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -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] } } @@ -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}' } /**