Skip to content

Commit bea7335

Browse files
committed
improve benchmark, optimize for case 3 too.
1 parent c93b86b commit bea7335

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

benchmark/error/format-list.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,36 @@ const bench = common.createBenchmark(main, {
99
'a',
1010
'a,b',
1111
'a,b,c',
12+
'a,b,c,d',
13+
],
14+
type: [
15+
'undefined',
16+
'and',
17+
'or',
1218
],
1319
}, {
1420
flags: ['--expose-internals'],
1521
});
1622

17-
function main({ n, input }) {
23+
function main({ n, input, type }) {
1824
const {
1925
formatList,
2026
} = require('internal/errors');
2127

2228
const list = input.split(',');
29+
30+
if (type === 'undefined') {
31+
bench.start();
32+
for (let i = 0; i < n; ++i) {
33+
formatList(list);
34+
}
35+
bench.end(n);
36+
return;
37+
}
38+
2339
bench.start();
2440
for (let i = 0; i < n; ++i) {
25-
formatList(list);
41+
formatList(list, type);
2642
}
2743
bench.end(n);
2844
}

lib/internal/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ function formatList(array, type = 'and') {
906906
case 0: return '';
907907
case 1: return `${array[0]}`;
908908
case 2: return `${array[0]} ${type} ${array[1]}`;
909+
case 3: return `${array[0]}, ${array[1]}, ${type} ${array[2]}`;
909910
default:
910911
return `${ArrayPrototypeJoin(ArrayPrototypeSlice(array, 0, -1), ', ')}, ${type} ${array[array.length - 1]}`;
911912
}

test/parallel/test-error-format-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (!common.hasIntl) common.skip('missing Intl');
1111
const and = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
1212
const or = new Intl.ListFormat('en', { style: 'long', type: 'disjunction' });
1313

14-
const input = ['apple', 'banana', 'orange'];
14+
const input = ['apple', 'banana', 'orange', 'pear'];
1515
for (let i = 0; i < input.length; i++) {
1616
const slicedInput = input.slice(0, i);
1717
strictEqual(formatList(slicedInput), and.format(slicedInput));

0 commit comments

Comments
 (0)