From 5ad0250e845e5d503ad9b9d21ec9984191b4e7f9 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Thu, 13 Jan 2022 16:06:01 -0500 Subject: [PATCH] optimize `__createBinding` Reflect microsoft/TypeScript#46997: When the binding is itself one that was created by `__createBinding`, re-use its descriptor, which avoids piling multiple levels of getters in the case of multiple levels of exports. In addition, reuse a descriptor if the bindings is marked as non-writable and non-configurable, which makes a getter not necessary. (This can be done manually if needed, even though tsc doesn't do it now.) Could be considered as a fix for #165 -- first, this PR prevents piling up multiple layers of getters. Second, it allows a hack of adding if (typeof exports === "object") exports = Object.freeze(exports); to avoid getters altogether. (And in the future, tsc could mark `const` exports as non-writable and non-configurable which would make it possible to avoid this hack.) --- tslib.es6.js | 6 +++++- tslib.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tslib.es6.js b/tslib.es6.js index f7c687f..e6d7777 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -107,7 +107,11 @@ export function __generator(thisArg, body) { export var __createBinding = Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/tslib.js b/tslib.js index 2e4326a..2b7885c 100644 --- a/tslib.js +++ b/tslib.js @@ -153,7 +153,11 @@ var __createBinding; __createBinding = Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k];