Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions benchmark/util/strip-vt-control-characters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const common = require('../common.js');

const { stripVTControlCharacters } = require('node:util');
const assert = require('node:assert');

const bench = common.createBenchmark(main, {
input: ['noAnsi-short', 'noAnsi-long', 'ansi-short', 'ansi-long'],
n: [1e6],
});

function main({ input, n }) {
let str;
switch (input) {
case 'noAnsi-short':
str = 'This is a plain text string without any ANSI codes';
break;
case 'noAnsi-long':
str = 'Long plain text without ANSI. '.repeat(333);
break;
case 'ansi-short':
str = '\u001B[31mHello\u001B[39m';
break;
case 'ansi-long':
str = ('\u001B[31m' + 'colored text '.repeat(10) + '\u001B[39m').repeat(10);
break;
}

bench.start();
for (let i = 0; i < n; i++) {
const result = stripVTControlCharacters(str);
assert.ok(typeof result === 'string');
}
bench.end(n);
}
7 changes: 7 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,13 @@ if (internalBinding('config').hasIntl) {
function stripVTControlCharacters(str) {
validateString(str, 'str');

// Short-circuit: all ANSI escape sequences start with either
// ESC (\u001B, 7-bit) or CSI (\u009B, 8-bit) introducer.
// If neither is present, the string has no VT control characters.
if (!StringPrototypeIncludes(str, '\u001B') &&
!StringPrototypeIncludes(str, '\u009B'))
return str;
Comment on lines +3042 to +3044
Copy link
Member

@gurgunday gurgunday Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are doing two passes here, why not use a simple regex here that only checks these 2 characters in one pass?

Suggested change
if (!StringPrototypeIncludes(str, '\u001B') &&
!StringPrototypeIncludes(str, '\u009B'))
return str;
if (!RegExpPrototypeTest(/[\u001B\u009B]/, str))
return str;

Did you test the performance of this? I think this would be even faster

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unlikely that a Regex test will be faster.

Possibly this is better:

if (StringPrototypeIndexOf(str, '\u001B') === -1 &&
    StringPrototypeIndexOf(str, '\u009B') === -1)


return RegExpPrototypeSymbolReplace(ansi, str, '');
}

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ assert.throws(() => {
message: 'The "str" argument must be of type string.' +
common.invalidArgTypeHelper({})
});

// stripVTControlCharacters: fast path returns input when no ANSI codes
assert.strictEqual(util.stripVTControlCharacters('hello'), 'hello');
assert.strictEqual(util.stripVTControlCharacters(''), '');

// stripVTControlCharacters: strips 7-bit ESC sequences
assert.strictEqual(util.stripVTControlCharacters('\u001B[31mfoo\u001B[39m'), 'foo');

// stripVTControlCharacters: strips 8-bit CSI sequences
assert.strictEqual(util.stripVTControlCharacters('\u009B31mfoo\u009B39m'), 'foo');
Loading