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
Improve performance #16
Comments
Yes, you're probably better off using https://github.com/sindresorhus/ansi-styles for high-performance situations. It will probably be faster than Though I'll keep this open to remind me about doing a perf review of Chalk. There might be some optimizations to be made. |
Thanks for the recommendation. I'll take a look. |
@CrabDude I made an alternate implementation that might be faster. It should be a drop-in replacement for https://github.com/aceface/crayon Can you try running it on your codebase and see if things run faster? If you don't a ton of chaining, it should be significantly faster (turning 100000 simple strings red was 31ms vs 7s with chalk for me) @sindresorhus take a look at the code. In general, I think the |
Definitely. I'll try it out this weekend and report back. On Friday, May 23, 2014, Charlie Cheever notifications@github.com wrote:
|
- Precomputed style function - Skip arguments to array + join if there's only one argument (the common case) - Merge multiple return statements to one To calculate the performance benefit: ```javascript var chalk = require('./index.js'); console.time('100000 iterations'); for (var i = 0; i < 100000; i++) { chalk.red('A string that is about 80 characters long (normal use I think?)'); } console.timeEnd('100000 iterations'); ``` Running this before this commit: ```shell for i in {1..5}; do node time.js; done 100000 iterations: 19485ms 100000 iterations: 18933ms 100000 iterations: 19365ms 100000 iterations: 19332ms 100000 iterations: 18660ms ``` After: ```shell 100000 iterations: 268ms 100000 iterations: 261ms 100000 iterations: 264ms 100000 iterations: 259ms 100000 iterations: 254ms ``` Performance gain, taking the middle result of both: ```shell 19332 / 261 = 74.~ ``` Closes chalk#16
- Precomputed style function - Skip arguments to array + join if there's only one argument (the common case) - Merge multiple return statements to one To calculate the performance benefit: ```javascript var chalk = require('./index.js'); console.time('100000 iterations'); for (var i = 0; i < 100000; i++) { chalk.red('A string that is about 80 characters long (normal use I think?)'); } console.timeEnd('100000 iterations'); ``` Running this before this commit: ```shell for i in {1..5}; do node time.js; done 100000 iterations: 19485ms 100000 iterations: 18933ms 100000 iterations: 19365ms 100000 iterations: 19332ms 100000 iterations: 18660ms ``` After: ```shell 100000 iterations: 268ms 100000 iterations: 261ms 100000 iterations: 264ms 100000 iterations: 259ms 100000 iterations: 254ms ``` Performance gain, taking the middle result of both: ```shell 19332 / 261 = 74.~ ``` Closes chalk#16
- Precomputed style function - Skip arguments to array + join if there's only one argument (the common case) - Merge multiple return statements to one To calculate the performance benefit: ```javascript var chalk = require('./index.js'); console.time('100000 iterations'); for (var i = 0; i < 100000; i++) { chalk.red('A string that is about 80 characters long (normal use I think?)'); } console.timeEnd('100000 iterations'); ``` Running this before this commit: ```shell for i in {1..5}; do node time.js; done 100000 iterations: 19485ms 100000 iterations: 18933ms 100000 iterations: 19365ms 100000 iterations: 19332ms 100000 iterations: 18660ms ``` After: ```shell 100000 iterations: 268ms 100000 iterations: 261ms 100000 iterations: 264ms 100000 iterations: 259ms 100000 iterations: 254ms ``` Performance gain, taking the middle result of both: ```shell 19332 / 261 = 74.~ ``` Closes chalk#16
- Precomputed style function - Skip arguments to array + join if there's only one argument (the common case) - Merge multiple return statements to one To calculate the performance benefit: ```javascript var chalk = require('./index.js'); console.time('100000 iterations'); for (var i = 0; i < 100000; i++) { chalk.red('A string that is about 80 characters long (normal use I think?)'); } console.timeEnd('100000 iterations'); ``` Running this before this commit: ```shell for i in {1..5}; do node time.js; done 100000 iterations: 19485ms 100000 iterations: 18933ms 100000 iterations: 19365ms 100000 iterations: 19332ms 100000 iterations: 18660ms ``` After: ```shell 100000 iterations: 268ms 100000 iterations: 261ms 100000 iterations: 264ms 100000 iterations: 259ms 100000 iterations: 254ms ``` Performance gain, taking the middle result of both: ```shell 19332 / 261 = 74.~ ``` Closes chalk#16
- Precomputed style function - Skip arguments to array + join if there's only one argument (the common case) - Merge multiple return statements to one To calculate the performance benefit: ```javascript var chalk = require('./index.js'); console.time('100000 iterations'); for (var i = 0; i < 100000; i++) { chalk.red('A string that is about 80 characters long (normal use I think?)'); } console.timeEnd('100000 iterations'); ``` Running this before this commit: ```shell for i in {1..5}; do node time.js; done 100000 iterations: 19485ms 100000 iterations: 18933ms 100000 iterations: 19365ms 100000 iterations: 19332ms 100000 iterations: 18660ms ``` After: ```shell 100000 iterations: 268ms 100000 iterations: 261ms 100000 iterations: 264ms 100000 iterations: 259ms 100000 iterations: 254ms ``` Performance gain, taking the middle result of both: ```shell 19332 / 261 = 74.~ ``` Closes chalk#16
I don't have specific other than that I was upgrading my server's trycatch version and it was very slow. Over a couple hours, I tracked the issue down to trycatch's coloring and then to the chalk module itself.
Removing just the line that adds coloring took my 3k unit tests from 30s down to 10s. The colors module doesn't have this problem. Not sure what the difference is. Looked through the source, but it looked like every time a color is accessed you're doing a dynamic getter?
The text was updated successfully, but these errors were encountered: