Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
estliberitas opened this issue Feb 17, 2016 · 4 comments
Closed

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

estliberitas opened this issue Feb 17, 2016 · 4 comments
Labels
performance Issues and PRs related to the performance of Node.js. util Issues and PRs related to the built-in util module.

Comments

@estliberitas
Copy link
Contributor

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.

@estliberitas
Copy link
Contributor Author

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.

@mscdex mscdex added util Issues and PRs related to the built-in util module. performance Issues and PRs related to the performance of Node.js. labels Feb 17, 2016
@andrasq
Copy link

andrasq commented Jul 8, 2016

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.)

@andrasq
Copy link

andrasq commented Jul 11, 2016

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

@evanlucas
Copy link
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
Labels
performance Issues and PRs related to the performance of Node.js. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

4 participants