Skip to content

Compiling code with a large template literal without placeholders is slower than an equivalent string literal #41448

@merceyz

Description

@merceyz

Version

v17.3.0

Platform

Linux DESKTOP 5.10.60.1-microsoft-standard-WSL2 #1 SMP Wed Aug 25 23:20:18 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

vm

Though most likely deps/V8

What steps will reproduce the bug?

const { compileFunction } = require(`vm`);

const value = `a`.repeat(1024 * 1024 * 5);

console.time(`string`);
for (let i = 0; i < 1000; i++) {
	compileFunction(`"${value}"`, [], {});
}
console.timeEnd(`string`);

console.time(`template`);
for (let i = 0; i < 1000; i++) {
	compileFunction(`\`${value}\``, [], {});
}
console.timeEnd(`template`);

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

The amount of time it takes to compile should ideally be the same.

What do you see instead?

Compiling the template literal is significantly slower

string: 19.088s
template: 26.216s

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    linuxIssues and PRs related to the Linux platform.performanceIssues and PRs related to the performance of Node.js.v8 engineIssues and PRs related to the V8 dependency.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions