X Tutup
Skip to content

perf: 2.7x speedup for 2-argument calls using string concat instead of join() #669

@mdhamed238

Description

@mdhamed238

Summary

The createBuilder function uses arguments_.join(' ') for multi-argument calls. For the common 2-argument case, using direct string concatenation instead could provide a ~2.7x speedup.

Why is join() slow for 2 arguments?

arguments_.join(' ')  // Current
  • Array iteration (even for just 2 items)
  • Method dispatch overhead
arguments_[0] + ' ' + arguments_[1]  // Faster alternative
  • V8 primitive operation, JIT-inlined

Use Case

The 2-argument pattern is common:

chalk.red('Error:', message)
chalk.blue('Count:', count)
chalk.green('User:', username)

Environment

  • Node.js: v20+
  • chalk: 5.6.2
  • OS: macOS (Apple M1)

Happy to submit a PR with benchmarks if this optimization is welcome!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup