Performance optimizations (ca. Factor 75)
- 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 #16
This commit is contained in:
parent
eff96c2c15
commit
b0523a4438
1 changed files with 20 additions and 20 deletions
40
index.js
40
index.js
|
|
@ -25,32 +25,32 @@ var styles = (function () {
|
||||||
return ret;
|
return ret;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
function applyStyle() {
|
||||||
|
// support varags, but simply cast to string in case there's only one arg
|
||||||
|
var str = arguments.length === 1 ? arguments[0] + '' : [].slice.call(arguments).join(' ');
|
||||||
|
|
||||||
|
if (!chalk.enabled || !str) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
return applyStyle._styles.reduce(function (str, name) {
|
||||||
|
var code = ansiStyles[name];
|
||||||
|
// Replace any instances already present with a re-opening code
|
||||||
|
// otherwise only the part of the string until said closing code
|
||||||
|
// will be colored, and the rest will simply be 'plain'.
|
||||||
|
return code.open + str.replace(code.closeRe, code.open) + code.close;
|
||||||
|
}, str) ;
|
||||||
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
var ret = {};
|
var ret = {};
|
||||||
|
|
||||||
Object.keys(styles).forEach(function (name) {
|
Object.keys(styles).forEach(function (name) {
|
||||||
|
var style = defineProps(applyStyle, styles);
|
||||||
ret[name] = {
|
ret[name] = {
|
||||||
get: function () {
|
get: function () {
|
||||||
var obj = defineProps(function self() {
|
style._styles = [];
|
||||||
var str = [].slice.call(arguments).join(' ');
|
return style[name];
|
||||||
|
|
||||||
if (!chalk.enabled) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
return self._styles.reduce(function (str, name) {
|
|
||||||
var code = ansiStyles[name];
|
|
||||||
return str ? code.open +
|
|
||||||
// Replace any instances already present with a re-opening code
|
|
||||||
// otherwise only the part of the string until said closing code
|
|
||||||
// will be colored, and the rest will simply be 'plain'.
|
|
||||||
str.replace(code.closeRe, code.open) + code.close : '';
|
|
||||||
}, str);
|
|
||||||
}, styles);
|
|
||||||
|
|
||||||
obj._styles = [];
|
|
||||||
|
|
||||||
return obj[name];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue