Skip to content

Commit

Permalink
perf: partially replace .concat with .push
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Jul 28, 2021
1 parent e4de256 commit 79b1306
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 43 deletions.
6 changes: 5 additions & 1 deletion packages/babel-cli/src/babel-external-helpers.ts
Expand Up @@ -10,7 +10,11 @@ function collect(

const values = value.split(",");

return previousValue ? previousValue.concat(values) : values;
if (previousValue) {
Array.prototype.push.apply(previousValue, values);
return previousValue;
}
return values;
}

commander.option(
Expand Down
9 changes: 7 additions & 2 deletions packages/babel-cli/src/babel/options.ts
Expand Up @@ -192,7 +192,8 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
let filenames = commander.args.reduce(function (globbed, input) {
let files = glob.sync(input);
if (!files.length) files = [input];
return globbed.concat(files);
Array.prototype.push.apply(globbed, files);
return globbed;
}, []);

filenames = Array.from(new Set(filenames));
Expand Down Expand Up @@ -353,5 +354,9 @@ function collect(

const values = value.split(",");

return previousValue ? previousValue.concat(values) : values;
if (previousValue) {
Array.prototype.push.apply(previousValue, values);
return previousValue;
}
return values;
}
3 changes: 2 additions & 1 deletion packages/babel-helper-define-map/src/index.js
Expand Up @@ -42,7 +42,8 @@ export function push(
if (node.decorators) {
const decorators = (map.decorators =
map.decorators || t.arrayExpression([]));
decorators.elements = decorators.elements.concat(
Array.prototype.push.apply(
decorators.elements,
node.decorators.map(dec => dec.expression).reverse(),
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -316,7 +316,7 @@ helpers.extends = helper("7.0.0-beta.0")`
}
`;

// This old helper can be removed in babel v8
// TODO(babel-8): This old helper can be removed in babel v8
helpers.objectSpread = helper("7.0.0-beta.0")`
import defineProperty from "defineProperty";
Expand All @@ -325,7 +325,7 @@ helpers.objectSpread = helper("7.0.0-beta.0")`
var source = (arguments[i] != null) ? Object(arguments[i]) : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
Array.prototype.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-node/src/_babel-node.js
Expand Up @@ -21,7 +21,11 @@ function collect(value, previousValue): Array<string> {

const values = value.split(",");

return previousValue ? previousValue.concat(values) : values;
if (previousValue) {
Array.prototype.push.apply(previousValue, values);
return previousValue;
}
return values;
}

program.option("-e, --eval [script]", "Evaluate script");
Expand Down Expand Up @@ -197,7 +201,7 @@ if (program.eval || program.print) {
}

// add back on node and concat the sliced args
process.argv = ["node"].concat(args);
process.argv = ["node", ...args];
process.execArgv.push(fileURLToPath(import.meta.url));

Module.runMain();
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-node/src/babel-node.js
Expand Up @@ -8,7 +8,7 @@ import path from "path";
import child_process from "child_process";
import { fileURLToPath } from "url";

let args = [
const args = [
path.join(path.dirname(fileURLToPath(import.meta.url)), "_babel-node"),
];

Expand Down Expand Up @@ -69,7 +69,7 @@ getV8Flags(async function (err, v8Flags) {

// append arguments passed after --
if (argSeparator > -1) {
args = args.concat(userArgs);
Array.prototype.push.apply(args, userArgs);
}

try {
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -1963,7 +1963,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>

if (failed && valid.length === 1) {
this.state = state;
this.state.noArrowAt = noArrowAt.concat(valid[0].start);
noArrowAt.push(valid[0].start);
this.state.noArrowAt = noArrowAt;
({ consequent, failed } = this.tryParseConditionalConsequent());
}
}
Expand Down
Expand Up @@ -47,7 +47,7 @@ export default declare(api => {
}

// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
Array.prototype.push.apply(block.body, node.body.body);

t.inherits(loop, node);
t.inherits(loop.body, node.body);
Expand Down
Expand Up @@ -157,7 +157,7 @@ function applyTargetDecorators(path, state, decoratedProps) {

WARNING_CALLS.add(node.value);

acc = acc.concat([
acc.push(
t.assignmentExpression(
"=",
t.cloneNode(descriptor),
Expand All @@ -184,9 +184,9 @@ function applyTargetDecorators(path, state, decoratedProps) {
]),
]),
),
]);
);
} else {
acc = acc.concat(
acc.push(
t.callExpression(state.addHelper("applyDecoratedDescriptor"), [
t.cloneNode(target),
t.cloneNode(property),
Expand Down
8 changes: 5 additions & 3 deletions packages/babel-plugin-transform-classes/src/transformClass.ts
Expand Up @@ -136,7 +136,8 @@ export default function transformClass(

if (classState.userConstructor) {
const { constructorBody, userConstructor, construct } = classState;
constructorBody.body = constructorBody.body.concat(
Array.prototype.push.apply(
constructorBody.body,
userConstructor.body.body,
);
t.inherits(construct, userConstructor);
Expand Down Expand Up @@ -695,7 +696,7 @@ export default function transformClass(

extractDynamicKeys();

let { body } = classState;
const { body } = classState;
const { closureParams, closureArgs } = setupClosureParamsArgs();

buildBody();
Expand All @@ -712,7 +713,8 @@ export default function transformClass(
);
}

body = body.concat(
Array.prototype.push.apply(
body,
classState.staticPropBody.map(fn => fn(t.cloneNode(classState.classRef))),
);

Expand Down
Expand Up @@ -24,7 +24,7 @@ export default function transformWithoutHelper(loose, path, state) {
}

// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
Array.prototype.push.apply(block.body, node.body.body);

t.inherits(loop, node);
t.inherits(loop.body, node.body);
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-plugin-transform-modules-systemjs/src/index.js
Expand Up @@ -295,7 +295,7 @@ export default declare((api, options) => {
const exportMap = Object.create(null);
const modules = [];

let beforeBody = [];
const beforeBody = [];
const setters = [];
const sources = [];
const variableIds = [];
Expand Down Expand Up @@ -475,7 +475,7 @@ export default declare((api, options) => {
}

modules.forEach(function (specifiers) {
let setterBody = [];
const setterBody = [];
const target = scope.generateUid(specifiers.key);

for (let specifier of specifiers.imports) {
Expand Down Expand Up @@ -540,7 +540,8 @@ export default declare((api, options) => {
}
}

setterBody = setterBody.concat(
Array.prototype.push.apply(
setterBody,
constructExportCall(
path,
t.identifier(exportIdent),
Expand Down Expand Up @@ -589,7 +590,8 @@ export default declare((api, options) => {
}

if (exportNames.length) {
beforeBody = beforeBody.concat(
Array.prototype.push.apply(
beforeBody,
constructExportCall(
path,
t.identifier(exportIdent),
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-parameters/src/rest.js
Expand Up @@ -305,7 +305,8 @@ export default function convertFunctionRest(path) {
return true;
}

state.references = state.references.concat(
Array.prototype.push.apply(
state.references,
state.candidates.map(({ path }) => path),
);

Expand Down
37 changes: 22 additions & 15 deletions packages/babel-traverse/src/path/family.ts
Expand Up @@ -46,7 +46,9 @@ function addCompletionRecords(
records: Completion[],
context: CompletionContext,
): Completion[] {
if (path) return records.concat(_getCompletionRecords(path, context));
if (path) {
Array.prototype.push.apply(records, _getCompletionRecords(path, context));
}
return records;
}

Expand All @@ -73,9 +75,9 @@ function completionRecordForSwitch(
if (normalCompletions.length) {
lastNormalCompletions = normalCompletions;
}
records = records.concat(breakCompletions);
Array.prototype.push.apply(records, breakCompletions);
}
records = records.concat(lastNormalCompletions);
Array.prototype.push.apply(records, lastNormalCompletions);
return records;
}

Expand Down Expand Up @@ -117,7 +119,7 @@ function getStatementListCompletion(
paths: NodePath[],
context: CompletionContext,
): Completion[] {
let completions = [];
const completions = [];
if (context.canHaveBreak) {
let lastNormalCompletions = [];
for (let i = 0; i < paths.length; i++) {
Expand Down Expand Up @@ -155,11 +157,11 @@ function getStatementListCompletion(
// When we have seen normal completions from the last statement
// it is safe to stop populating break and mark normal completions as break
normalCompletionToBreak(lastNormalCompletions);
completions = completions.concat(lastNormalCompletions);
Array.prototype.push.apply(completions, lastNormalCompletions);
// Declarations have empty completion record, however they can not be nested
// directly in return statement, i.e. `return (var a = 1)` is invalid.
if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
completions = completions.concat(statementCompletions);
Array.prototype.push.apply(completions, statementCompletions);
replaceBreakStatementInBreakCompletion(
statementCompletions,
/* reachable */ true,
Expand All @@ -170,7 +172,7 @@ function getStatementListCompletion(
/* reachable */ false,
);
} else {
completions = completions.concat(statementCompletions);
Array.prototype.push.apply(completions, statementCompletions);
if (!context.shouldPopulateBreak) {
replaceBreakStatementInBreakCompletion(
statementCompletions,
Expand All @@ -181,9 +183,10 @@ function getStatementListCompletion(
break;
}
if (i === paths.length - 1) {
completions = completions.concat(statementCompletions);
Array.prototype.push.apply(completions, statementCompletions);
} else {
completions = completions.concat(
Array.prototype.push.apply(
completions,
statementCompletions.filter(c => c.type === BREAK_COMPLETION),
);
lastNormalCompletions = statementCompletions.filter(
Expand All @@ -202,7 +205,7 @@ function getStatementListCompletion(
(pathCompletions.length === 1 &&
!pathCompletions[0].path.isVariableDeclaration())
) {
completions = completions.concat(pathCompletions);
Array.prototype.push.apply(completions, pathCompletions);
break;
}
}
Expand All @@ -227,7 +230,8 @@ function _getCompletionRecords(
// @ts-expect-error(flow->ts): todo
records = addCompletionRecords(path.get("body"), records, context);
} else if (path.isProgram() || path.isBlockStatement()) {
records = records.concat(
Array.prototype.push.apply(
records,
// @ts-expect-error(flow->ts): todo
getStatementListCompletion(path.get("body"), context),
);
Expand All @@ -241,7 +245,8 @@ function _getCompletionRecords(
} else if (path.isSwitchStatement()) {
records = completionRecordForSwitch(path.get("cases"), records, context);
} else if (path.isSwitchCase()) {
records = records.concat(
Array.prototype.push.apply(
records,
getStatementListCompletion(path.get("consequent"), {
canHaveBreak: true,
shouldPopulateBreak: false,
Expand Down Expand Up @@ -451,7 +456,7 @@ export function getBindingIdentifierPaths(
[x: string]: NodePath;
} {
const path = this;
let search = [].concat(path);
const search = [path];
const ids = Object.create(null);

while (search.length) {
Expand Down Expand Up @@ -493,8 +498,10 @@ export function getBindingIdentifierPaths(
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const child = id.get(key);
if (Array.isArray(child) || child.node) {
search = search.concat(child);
if (Array.isArray(child)) {
Array.prototype.push.apply(search, child);
} else if (child.node) {
search.push(child);
}
}
}
Expand Down
Expand Up @@ -84,7 +84,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
}*/

// add back on function constant violations since we can't track calls
constantViolations = constantViolations.concat(functionConstantViolations);
Array.prototype.push.apply(constantViolations, functionConstantViolations);

// push on inferred types of violated paths
for (const violation of constantViolations) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/src/comments/addComments.ts
Expand Up @@ -16,7 +16,7 @@ export default function addComments<T extends t.Node>(
if (type === "leading") {
node[key] = comments.concat(node[key]);
} else {
node[key] = node[key].concat(comments);
Array.prototype.push.apply(node[key], comments);
}
} else {
node[key] = comments;
Expand Down
Expand Up @@ -48,7 +48,7 @@ export default function removeTypeDuplicates(

if (isUnionTypeAnnotation(node)) {
if (typeGroups.indexOf(node.types) < 0) {
nodes = nodes.concat(node.types);
Array.prototype.push.apply(nodes, node.types);
typeGroups.push(node.types);
}
continue;
Expand Down
Expand Up @@ -41,7 +41,7 @@ export default function removeTypeDuplicates(

if (isTSUnionType(node)) {
if (typeGroups.indexOf(node.types) < 0) {
nodes = nodes.concat(node.types);
Array.prototype.push.apply(nodes, node.types);
typeGroups.push(node.types);
}
continue;
Expand Down

0 comments on commit 79b1306

Please sign in to comment.