Skip to content

Commit

Permalink
perf(transformer): look up SymbolId for require only once (#6192)
Browse files Browse the repository at this point in the history
When inserting `var x = require('y');` statements in `ModuleImports`, look up the binding for `require` only once, rather than for each statement.
  • Loading branch information
overlookmotel committed Oct 1, 2024
1 parent a7ed29e commit da2b2a4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ impl<'a, 'ctx> ModuleImports<'a, 'ctx> {
imports: &mut IndexMap<Atom<'a>, Vec<NamedImport<'a>>>,
ctx: &mut TraverseCtx<'a>,
) {
let stmts = imports.drain(..).map(|(source, names)| Self::get_require(source, names, ctx));
if imports.is_empty() {
return;
}

let require_symbol_id = ctx.scopes().get_root_binding("require");
let stmts = imports
.drain(..)
.map(|(source, names)| Self::get_require(source, names, require_symbol_id, ctx));
self.ctx.top_level_statements.insert_statements(stmts);
}

Expand Down Expand Up @@ -116,12 +123,16 @@ impl<'a, 'ctx> ModuleImports<'a, 'ctx> {
fn get_require(
source: Atom<'a>,
names: std::vec::Vec<NamedImport<'a>>,
require_symbol_id: Option<SymbolId>,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let var_kind = VariableDeclarationKind::Var;
let symbol_id = ctx.scopes().get_root_binding("require");
let ident =
ctx.create_reference_id(SPAN, Atom::from("require"), symbol_id, ReferenceFlags::read());
let ident = ctx.create_reference_id(
SPAN,
Atom::from("require"),
require_symbol_id,
ReferenceFlags::read(),
);
let callee = ctx.ast.expression_from_identifier_reference(ident);

let args = {
Expand Down

0 comments on commit da2b2a4

Please sign in to comment.