Skip to content

Commit 840a5ea

Browse files
Improve performance (#49)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 2b8c961 commit 840a5ea

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import ansiRegex from 'ansi-regex';
22

3+
const regex = ansiRegex();
4+
35
export default function stripAnsi(string) {
46
if (typeof string !== 'string') {
57
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
68
}
79

8-
return string.replace(ansiRegex(), '');
10+
// Even though the regex is global, we don't need to reset the `.lastIndex`
11+
// because unlike `.exec()` and `.test()`, `.replace()` does it automatically
12+
// and doing it manually has a performance penalty.
13+
return string.replace(regex, '');
914
}

0 commit comments

Comments
 (0)