chalk/chalk

Improve require speed #180

shnhrrsn posted onGitHub

I tested this by creating a new project and adding just time-require and chalk. I then created a simple time.js file with the following contents:

require('time-require')
require('chalk')

Results for Chalk 1.x (1.1.3 as of this posting):

Start time: (2017-07-22 16:50:03 UTC) [treshold=1%]
#  module                                                                time  %
1  pretty-ms (node_modules/pretty-ms/index.js)                            1ms  ▇▇▇▇▇▇▇▇ 11%
2  ansi-styles (node_modules/time-re...ules/ansi-styles/ansi-styles.js)   1ms  ▇▇▇▇▇▇▇▇ 11%
3  has-color (node_modules/has-color/index.js)                            1ms  ▇▇▇▇▇▇▇▇ 11%
4  chalk (node_modules/time-require/node_modules/chalk/index.js)          3ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 33%
5  escape-string-regexp (node_modules/escape-string-regexp/index.js)      1ms  ▇▇▇▇▇▇▇▇ 11%
6  ansi-styles (node_modules/ansi-styles/index.js)                        1ms  ▇▇▇▇▇▇▇▇ 11%
7  has-ansi (node_modules/has-ansi/index.js)                              1ms  ▇▇▇▇▇▇▇▇ 11%
8  chalk (node_modules/chalk/index.js)                                    3ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 33%
Total require(): 16
Total time: 9ms

Results for Chalk 2.x (2.0.1 as of this posting):

 #  module                                                                time  %
 1  text-table (node_modules/text-table/index.js)                          1ms  ▇▇▇▇▇ 6%
 2  date-time (node_modules/date-time/index.js)                            1ms  ▇▇▇▇▇ 6%
 3  parse-ms (node_modules/parse-ms/index.js)                              1ms  ▇▇▇▇▇ 6%
 4  pretty-ms (node_modules/pretty-ms/index.js)                            1ms  ▇▇▇▇▇ 6%
 5  ansi-styles (node_modules/time-re...ules/ansi-styles/ansi-styles.js)   1ms  ▇▇▇▇▇ 6%
 6  has-color (node_modules/has-color/index.js)                            1ms  ▇▇▇▇▇ 6%
 7  chalk (node_modules/time-require/node_modules/chalk/index.js)          2ms  ▇▇▇▇▇▇▇▇▇ 13%
 8  escape-string-regexp (node_modules/escape-string-regexp/index.js)      1ms  ▇▇▇▇▇ 6%
 9  ./conversions (node_modules/color-convert/conversions.js)              2ms  ▇▇▇▇▇▇▇▇▇ 13%
10  color-convert (node_modules/color-convert/index.js)                    5ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 31%
11  ansi-styles (node_modules/ansi-styles/index.js)                        6ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 38%
12  os (os)                                                                1ms  ▇▇▇▇▇ 6%
13  supports-color (node_modules/supports-color/index.js)                  1ms  ▇▇▇▇▇ 6%
14  ./templates.js (node_modules/chalk/templates.js)                       1ms  ▇▇▇▇▇ 6%
15  chalk (node_modules/chalk/index.js)                                   10ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 63%
Total require(): 20
Total time: 16ms

I ran the tests several times locally and the results were all within 1ms each time.

For long running processes, the difference here is obviously negligible so I'll understand if this gets closed out.

That being said, for CLI utils where boot time and responsiveness are far more important, it's unfortunate that just using 2.x adds an additional 5ms+ on average that you can't get back.


Not sure what you suggest we do about this.

The community complains about many tiny modules, and then gets mad when X module doesn't have all of these features out of the box. So we've struck a somewhat bearable middleground by thinking really hard about every dependency we include.

posted by Qix- over 7 years ago

Which Node.js version is this benchmarked against? Make sure you test with latest Node.js version.

posted by sindresorhus over 7 years ago

@Qix- Maybe we could lazy load color-convert only when it's actually used, so people only using the normal 16 colors don't have to pay the price of loading it?

posted by sindresorhus over 7 years ago

@sindresorhus Sure, that'd work.

posted by Qix- over 7 years ago

@issuehunt has funded $60.00 to this issue.


posted by IssueHuntBot about 6 years ago

I'll take this if @zeit/ncc solves it.

npm init -y
npm i time-require chalk
node -r time-require -r chalk -e ''
cd node_modules/chalk
npx @zeit/ncc build index.js
npx dot-json package.json main 'dist/index.js' 
node -r time-require -r chalk -e ''

Before/after ncc

<img width="794" alt="" src="https://user-images.githubusercontent.com/1402241/55139772-88d00800-5171-11e9-87ed-9bc83deb3c2e.png">

posted by bfred-it about 6 years ago

@sindresorhus has rewarded $54.00 to @stroncium. See it on IssueHunt

  • :moneybag: Total deposit: $60.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $6.00
posted by issuehunt-app[bot] over 5 years ago

Fund this Issue

$60.00
Rewarded

Rewarded pull request

Recent activities

stroncium was rewarded by sindresorhus for chalk/chalk# 180
over 5 years ago
stroncium submitted an output to  chalk/ chalk# 180
over 5 years ago