Skip to content

Commit 7b5e92f

Browse files
committed
perf: optimizing the issue of excessive initial memory usage(#2)
1 parent 8f6dd0a commit 7b5e92f

File tree

3 files changed

+104
-32
lines changed

3 files changed

+104
-32
lines changed

benchmark/measureMemory.cjs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function measureMemory() {
2+
const usage1 = process.memoryUsage();
3+
const clc = require('../src/index.js');
4+
clc.green('hello word');
5+
6+
const usage2 = process.memoryUsage();
7+
const mb = 1024 * 1024;
8+
9+
console.log(`Memory Useage(MB):`);
10+
console.log(` RSS:${(usage2.rss - usage1.rss) / mb}`);
11+
console.log(` heapTotal:${(usage2.heapTotal - usage1.heapTotal) / mb}`);
12+
console.log(` heapUsed:${(usage2.heapUsed - usage1.heapUsed) / mb}`);
13+
}
14+
15+
const argv = process.argv.slice(2);
16+
process.env.CLC_LOW_MEMORY = argv.includes('speed') ? '0' : '1';
17+
process.env.CLC_C256 = argv.includes('c256') ? '1' : '0';
18+
19+
measureMemory();

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"scripts": {
5555
"test": "node examples/index.js",
5656
"dr": "node benchmark/dry-run.mjs",
57-
"benchmark": "node benchmark/index.mjs"
57+
"benchmark": "node benchmark/index.mjs",
58+
"mem": "node benchmark/measureMemory.cjs"
5859
},
5960
"devDependencies": {
6061
"ansi-colors": "^4.1.3",

src/index.js

+83-31
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,33 @@ var colorList = {
5555
bgCyanBright: [106, 49],
5656
bgWhiteBright: [107, 49],
5757
};
58-
if (typeof process === 'undefined' || !process.env) { var process = { env: {}, argv: ['--color'] }; }
58+
var c256List = {};
59+
if (typeof process === 'undefined' || !process.env) { globalThis.process = { env: {}, argv: ['--color'] }; }
60+
var isLowMemory = process.env.CLC_LOW_MEMORY == '1';
61+
var isC256Disabled = process.env.CLC_C256 == '0';
5962
var isDisabled = process.env.NO_COLOR || process.argv.includes('--no-color');
6063
var isSupported = !isDisabled && (process.env.FORCE_COLOR ||
61-
process.platform === 'win32' ||
62-
process.argv.includes('--color') ||
63-
(eval(`require('tty')`).isatty(1) && process.env.TERM !== 'dumb') ||
64-
process.env.CI);
64+
process.platform === 'win32' ||
65+
process.argv.includes('--color') ||
66+
(eval(`require('tty')`).isatty(1) && process.env.TERM !== 'dumb') ||
67+
process.env.CI);
6568
var TObject = typeof Reflect === 'undefined' ? Object : Reflect;
6669
var fncache = {};
6770
function extend(fn, keys) {
6871
var prefix = keys.join('');
69-
Object.keys(colorList).forEach(function (key) {
70-
var cachekey = prefix + key;
71-
TObject.defineProperty(fn, key, {
72-
get() {
73-
if (!fncache[cachekey]) fncache[cachekey] = extend(function m(s) { return fn(color[key](s)) }, keys.concat(key));
74-
return fncache[cachekey];
75-
}
76-
});
72+
Object.keys(clc.list).forEach(function (key) {
73+
// if (keys.indexOf(key) !== -1) return;
74+
var cachekey = prefix + key;
75+
TObject.defineProperty(fn, key, {
76+
get() {
77+
if (!fncache[cachekey]) {
78+
fncache[cachekey] = extend(function m(s) {
79+
return fn(color[key](s));
80+
}, keys.concat(key));
81+
}
82+
return fncache[cachekey];
83+
},
84+
});
7785
});
7886
return fn;
7987
}
@@ -82,37 +90,81 @@ function replaceClose(str, open, close, idx) {
8290
var nextIdx = rest.indexOf(close);
8391
return str.substring(0, idx) + open + (~nextIdx ? replaceClose(rest, open, close, nextIdx) : rest);
8492
}
93+
function toString(s) {
94+
return s;
95+
}
8596
function getFn(colorType) {
86-
var cfg = colorList[colorType];
87-
if (!cfg || !isSupported) return function (str) { return String(str) };
88-
var open = cfg[0], close = cfg[1];
97+
var cfg = clc.list[colorType];
98+
if (!cfg || !isSupported) return toString;
99+
var open = cfg[0],
100+
close = cfg[1];
89101
return function (str) {
90102
if (str === '' || str == null) return '';
91103
str = '' + str;
92104
var idx = str.indexOf(close, open.length);
93105
return open + (idx > -1 && idx < str.length - 1 ? replaceClose(str, open, close, idx) : str) + close;
94-
}
106+
};
95107
}
96108
function color(str, colorType) { return getFn(colorType)(str); }
97-
color.list = colorList;
98109
function init() {
99-
Object.keys(colorList).forEach(function (key) { clc[key] = color[key] = extend(getFn(key), [key]) });
110+
var cache = {};
111+
if (!isLowMemory) {
112+
Object.keys(colorList).forEach(function (key) {
113+
clc[key] = color[key] = extend(getFn(key), [key]);
114+
});
115+
}
116+
Object.keys(clc.list).forEach(function (key) {
117+
if (!color[key]) {
118+
Object.defineProperty(color, key, {
119+
get() {
120+
if (!cache[key]) cache[key] = extend(getFn(key), [key]);
121+
return cache[key];
122+
},
123+
});
124+
}
125+
126+
if (!clc[key]) {
127+
Object.defineProperty(clc, key, {
128+
get() {
129+
return cache[key] || color[key];
130+
},
131+
});
132+
}
133+
});
100134
}
135+
136+
if (!isC256Disabled) {
137+
for (var i = 0; i < 256; i++) {
138+
c256List['c' + i] = ['38;5;' + i, 0];
139+
c256List['bg' + i] = ['48;5;' + i, 0];
140+
}
141+
}
142+
101143
var clc = {
102144
color: color,
103-
list: colorList,
104-
log(str, colorType) { console.log(color(str, colorType)) },
105-
isSupported() { return isSupported },
106-
enable() { isSupported = true; init(); },
107-
disable() { isSupported = false; init(); },
108-
strip(str) { return str.replace(/\x1b\[\d+m/gm, '') },
145+
list: Object.assign({}, colorList, c256List),
146+
log(str, colorType) {
147+
console.log(color(str, colorType));
148+
},
149+
isSupported() {
150+
return isSupported;
151+
},
152+
enable() {
153+
isSupported = true;
154+
init();
155+
},
156+
disable() {
157+
isSupported = false;
158+
init();
159+
},
160+
strip(str) {
161+
return str.replace(/\x1b\[\d+m/gm, '');
162+
},
109163
};
110-
for (var i = 0; i < 256; i++) {
111-
colorList['c' + i] = ['38;5;' + i, 0];
112-
colorList['bg' + i] = ['48;5;' + i, 0];
113-
}
114-
Object.keys(colorList).forEach(function (key) {
115-
colorList[key] = colorList[key].map(function (n) { return '\x1b[' + n + 'm' });
164+
color.list = clc.list;
165+
166+
Object.keys(clc.list).forEach(function (key) {
167+
clc.list[key] = clc.list[key].map(function (n) { return '\x1b[' + n + 'm' });
116168
clc.log[key] = function () {
117169
var arr = [];
118170
for (var i = 0; i < arguments.length; i++) arr.push(arguments[i]);

0 commit comments

Comments
 (0)