Repository navigation
util: possible way to speed up util.format() #5285
Description
Activity
I also found other issues which intend to optimize
util.format():- [WIP] lib: optimize util.format() by @bnoordhuis seems to be stalled
- util: Speed up the common case of formatting a single string. by @ChALkeR simplies common case with single string
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 withindexOf()it would either require small inner loop orsubstring()usage which actually gives another one called function per each gap between placeholders.- addedutilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.performanceIssues and PRs related to the performance of Node.js.Issues and PRs related to the performance of Node.js.
on Feb 17, 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.)Walking a string character by character is faster than using indexOf.
I modified qprintf to iterate over the string instead of usingindexOfand it sped up over 20%
(still usingsliceto 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 #5360Going to go ahead and close since util.format is no longer using String#replace. Performance has improved pretty significantly for it since then. Thanks!
Hi guys. While I was looking at sources... I found that
util.format()is based onString#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.:
indexOf('%')indexOf('%')Or, explain plz why it would not give actual speedup.