question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

multiple chalk functions share the same _styles array

See original GitHub issue

I’d been doing this with chalk 0.4:

var COLORS = {
  INFO: chalk.bold.green,
  WARN: chalk.bold.yellow,
  ERROR: chalk.bold.bgRed,
  // etc
}

Upgrading to 0.5, I find that all of the functions I’ve stored are all set to apply the styles of the last on in the object.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
seanmonstarcommented, Jul 9, 2014

As I fiddle on a patch for this, I can’t think of a way of providing the same API with the same performance. Each getter needs to return a new object (a function), and that function needs to have all the getters set. Object.defineProperty is brutal in a loop. Here’s the options I see:

  1. Live with the performance hit of defining getters on each function. It’s not bad at all if you cache the style into a variable, such var style = chalk.blue.bgRed.bold, since subsequent uses of style won’t be triggering new definePropertys. I’ve added a benchmark that shows with the style cached into a variable.
  2. Get ~8x performance of option 1, by setting __proto__ on the function to an object with the properties already defined. __proto__ is deprecated, in ES6 it will become Object.setPrototypeOf, but it can’t be remove any time soon if they don’t want to blow up the web. The docs for __proto__ state that it hurts performance, but comparing the benchmark with using Object.create() and chalk.red._('foo') showed it was actually slightly slower than this __proto__ hack.
  3. Say to hell it with Sean, just call everything off chalk (this is still much slower than caching a couple styles).

Here’s the benches with option 2 (and an optimization of not doing [].slice.call(arguments):

before
                      chalk
       1,286,288 op/s » single style
         943,220 op/s » several styles
         245,858 op/s » nested styles

after
                      chalk
       1,341,294 op/s » single style
         254,177 op/s » several styles
       1,843,002 op/s » cached styles
         298,379 op/s » nested styles
0reactions
jbnicolaicommented, Jul 9, 2014

Same here 😃 let me have one last look and I’ll merge it in

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to combine multiple inline style objects? - reactjs
For two static styles, doing StyleSheet.compose() outside the render function might be more efficient. It will produce a new static stylesheet that will...
Read more >
Styling console.log() Output With A Chalk-Inspired Formatter ...
To do this, I'm going to build off of yesterday's echo object. Only, instead of having just the two custom stylers - asWarning...
Read more >
Colorize your console output with Chalk! - Node.js ... - YouTube
Chalk NPM package link:https://www.npmjs.com/package/chalkIn this video I'll be demonstrating how you can use the hugely popular NPM package ...
Read more >
Chalk: Terminal String Styling Done Right - Morioh
Compose multiple styles using the chainable API log(chalk.blue. ... Data Driven Features | Calling JavaScript Functions | Calling Java Code | Commonly ...
Read more >
Functional Programming in JavaScript: Introduction and ...
Functional programming (FP) is a style of coding that's been ... A combinator has the same requirements as a pure function, plus one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found