-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add ASCII fast path to getStringWidth()
A lot of strings that are going to be passed to `getStringWidth()` are ASCII strings, for which the calculation is rather easy and calling into C++ can be skipped. confidence improvement accuracy (*) (**) (***) misc/getstringwidth.js n=100000 type='ascii' *** 328.99 % ±21.73% ±29.25% ±38.77% misc/getstringwidth.js n=100000 type='emojiseq' 2.94 % ±7.66% ±10.19% ±13.26% misc/getstringwidth.js n=100000 type='fullwidth' 4.70 % ±5.64% ±7.50% ±9.76% PR-URL: #29301 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
Showing
3 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['ascii', 'mixed', 'emojiseq', 'fullwidth'], | ||
n: [10e4] | ||
}, { | ||
flags: ['--expose-internals'] | ||
}); | ||
|
||
function main({ n, type }) { | ||
const { getStringWidth } = require('internal/readline/utils'); | ||
|
||
const str = ({ | ||
ascii: 'foobar'.repeat(100), | ||
mixed: 'foo'.repeat(100) + '😀' + 'bar'.repeat(100), | ||
emojiseq: '👨👨👧👦👨👩👦👦👨👩👧👧👩👩👧👦'.repeat(10), | ||
fullwidth: '你好'.repeat(150) | ||
})[type]; | ||
|
||
bench.start(); | ||
for (let j = 0; j < n; j += 1) | ||
getStringWidth(str); | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters