Skip to content

util: possible way to speed up util.format() #5285

Description

@estliberitas

Hi guys. While I was looking at sources... I found that util.format() is based on String#replace() with "replacer" function.

I am wondering if anyone has experimented with automata-based solution maybe composed with String#indexOf('%') (for faster moving from one char sequence to another).

If no, would you be interested in such an implementation? I'd benchmark following impls.:

  • automata w/ indexOf('%')
  • automata w/o indexOf('%')

Or, explain plz why it would not give actual speedup.

Activity

  1. estliberitas commented on Feb 17, 2016

    @estliberitas
    ContributorAuthor

    I also found other issues which intend to optimize util.format():

    Another one thing to consider would be concatenation of result string. Without indexOf() it's simple with just adding a char to result string (any perf. overhead here?), but with indexOf() it would either require small inner loop or substring() usage which actually gives another one called function per each gap between placeholders.

  2. added
    utilIssues and PRs related to the built-in util module.
    performanceIssues and PRs related to the performance of Node.js.
    on Feb 17, 2016
  3. andrasq commented on Jul 8, 2016

    @andrasq

    It's an interesting idea, but examining a string character by character is very fast in nodejs.
    I have a simple printf-like % argument interpolator (qprintf) that works as you describe
    (state machine that uses indexOf to find the next %), and util.format is 2x faster. (I should
    probably experiment with that, see if I can close the gap.)

  4. andrasq commented on Jul 11, 2016

    @andrasq

    Walking a string character by character is faster than using indexOf.
    I modified qprintf to iterate over the string instead of using indexOf and it sped up over 20%
    (still using slice to concat chunks of the format string, it's faster than appending chars).

    The stalled pull request might have been to help older versions of util.format.
    The current (v6) version is 5-10x faster than it was in v0.10 and v4.4, see the
    changes made in #5360

  5. evanlucas commented on Feb 16, 2017

    @evanlucas
    Contributor

    Going to go ahead and close since util.format is no longer using String#replace. Performance has improved pretty significantly for it since then. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performanceIssues and PRs related to the performance of Node.js.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions