Skip to content

util: improve perf for format

Checklist
  • tests and code linting passes
  • a test and/or benchmark is included
  • the commit message follows commit guidelines
Affected core subsystem(s)

util

Description of change

In the event the first argument to util.format is not a string, we were previously using Array#join. If we switch to string concatenation, we can improve the performance by (5%-100%). The largest improvement can be seen when the arguments are primitives that are not strings.

$ ./node benchmark/compare.js ./node ./node_before  -- util format
running ./node
util/format.js
running ./node_before
util/format.js

util/format.js n=1000000 type=string: ./node: 8109400 ./node_before: 8075700 ........ 0.42%
util/format.js n=1000000 type=number: ./node: 7745800 ./node_before: 8095700 ....... -4.32%
util/format.js n=1000000 type=object: ./node: 1892000 ./node_before: 1903900 ....... -0.62%
util/format.js n=1000000 type=unknown: ./node: 14278000 ./node_before: 13761000 ..... 3.76%
util/format.js n=1000000 type=no-replace: ./node: 3287200 ./node_before: 1639200 .. 100.54%
util/format.js n=1000000 type=no-replace-object: ./node: 75547 ./node_before: 70183 . 7.64%

Merge request reports

Loading