Skip to content
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

Closed
CrabDude opened this issue Apr 2, 2014 · 4 comments
Closed

Improve performance #16

CrabDude opened this issue Apr 2, 2014 · 4 comments

Comments

@CrabDude
Copy link

CrabDude commented Apr 2, 2014

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?

@sindresorhus
Copy link
Member

Yes, you're probably better off using https://github.com/sindresorhus/ansi-styles for high-performance situations. It will probably be faster than colors too.

Though I'll keep this open to remind me about doing a perf review of Chalk. There might be some optimizations to be made.

@CrabDude
Copy link
Author

CrabDude commented Apr 2, 2014

Thanks for the recommendation. I'll take a look.

@sindresorhus sindresorhus changed the title Chalk is incredibly slow compared to colors Improve performance May 10, 2014
This was referenced May 23, 2014
@ccheever
Copy link

@CrabDude I made an alternate implementation that might be faster. It should be a drop-in replacement for chalk and work exactly the same way.

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 defineProps for every style each time there is a call is what is slow, but would have to measure more to be sure.

@CrabDude
Copy link
Author

Definitely. I'll try it out this weekend and report back.

On Friday, May 23, 2014, Charlie Cheever notifications@github.com wrote:

@CrabDude https://github.com/CrabDude I made an alternate
implementation that might be faster. It should be a drop-in replacement for
chalk and work exactly the same way.

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 https://github.com/sindresorhus take a look at the code.
In general, I think the defineProps for every style each time there is a
call is what is slow, but would have to measure more to be sure.


Reply to this email directly or view it on GitHubhttps://github.com//issues/16#issuecomment-44069929
.

jbnicolai pushed a commit to jbnicolai/chalk that referenced this issue Jun 24, 2014
- 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
jbnicolai pushed a commit to jbnicolai/chalk that referenced this issue Jun 25, 2014
- 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
jbnicolai pushed a commit to jbnicolai/chalk that referenced this issue Jun 25, 2014
- 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
jbnicolai pushed a commit to jbnicolai/chalk that referenced this issue Jun 25, 2014
- 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
jbnicolai pushed a commit to jbnicolai/chalk that referenced this issue Jun 25, 2014
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants