Skip to content

Commit

Permalink
Use more efficient loops for const bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 16, 2021
1 parent b7a3933 commit e819692
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 23 deletions.
17 changes: 10 additions & 7 deletions src/finalisers/shared/getExportBlock.ts
Expand Up @@ -14,12 +14,13 @@ export function getExportBlock(
interop: GetInterop,
{
_,
s,
cnst,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getFunctionIntro,
getPropertyAccess,
n
n,
s
}: GenerateCodeSnippets,
t: string,
externalLiveBindings: boolean,
Expand Down Expand Up @@ -98,12 +99,14 @@ export function getExportBlock(
name: null
})}${name}[k]${directReturnFunctionRight}${n}${t}})`
: `exports[k]${_}=${_}${name}[k]`;
const copyPropertyIfNecessary = `{${n}${t}if${_}(k${_}!==${_}'default'${_}&&${_}!exports.hasOwnProperty(k))${_}${defineProperty}${s}${n}}`;
exportBlock +=
`Object.keys(${name}).forEach(${getFunctionIntro(['k'], {
isAsync: false,
name: null
})}{${n}` +
`${t}if${_}(k${_}!==${_}'default'${_}&&${_}!exports.hasOwnProperty(k))${_}${defineProperty}${s}${n}});`;
cnst === 'const'
? `for${_}(const k in ${name})${_}${copyPropertyIfNecessary}`
: `Object.keys(${name}).forEach(${getFunctionIntro(['k'], {
isAsync: false,
name: null
})}${copyPropertyIfNecessary});`;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/interopHelpers.ts
Expand Up @@ -153,6 +153,7 @@ const HELPER_GENERATORS: {
[MERGE_NAMESPACES_VARIABLE](t, snippets, liveBindings, freeze) {
const { _, directReturnFunctionRight, getDirectReturnFunctionLeft, getFunctionIntro, n } =
snippets;
// TODO Lukas use more efficient loops if we can use const and document it
return (
`function ${MERGE_NAMESPACES_VARIABLE}(n, m)${_}{${n}` +
`${t}m.forEach(${getDirectReturnFunctionLeft(['e'], {
Expand Down Expand Up @@ -194,6 +195,7 @@ const createNamespaceObject = (
: 'Object.create(null)'
};${n}` +
`${i}if${_}(e)${_}{${n}` +
// TODO Lukas use more efficient loops if we can use const and document it
`${i}${t}Object.keys(e).forEach(${getFunctionIntro(['k'], {
isAsync: false,
name: null
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -40,12 +40,12 @@ define(['exports', 'external'], (function (exports, foo$1) { 'use strict';

exports["default"] = main;
exports.syntheticMissing = synthetic.syntheticMissing;
Object.keys(foo$1).forEach(k => {
for (const k in foo$1) {
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
enumerable: true,
get: () => foo$1[k]
});
});
}

Object.defineProperty(exports, '__esModule', { value: true });

Expand Down
Expand Up @@ -44,9 +44,9 @@ const main = 42;

exports["default"] = main;
exports.syntheticMissing = synthetic.syntheticMissing;
Object.keys(foo$1).forEach(k => {
for (const k in foo$1) {
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
enumerable: true,
get: () => foo$1[k]
});
});
}
Expand Up @@ -41,12 +41,12 @@ const bundle = (function (exports, foo$1) {

exports["default"] = main;
exports.syntheticMissing = synthetic.syntheticMissing;
Object.keys(foo$1).forEach(k => {
for (const k in foo$1) {
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
enumerable: true,
get: () => foo$1[k]
});
});
}

Object.defineProperty(exports, '__esModule', { value: true });

Expand Down
Expand Up @@ -49,12 +49,12 @@

exports["default"] = main;
exports.syntheticMissing = synthetic.syntheticMissing;
Object.keys(foo$1).forEach(k => {
for (const k in foo$1) {
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
enumerable: true,
get: () => foo$1[k]
});
});
}

Object.defineProperty(exports, '__esModule', { value: true });

Expand Down

0 comments on commit e819692

Please sign in to comment.