Skip to content

Commit aefb68b

Browse files
author
Long Ho
committed
fix: remove custom unescaping of static message
Since ICU message format uses apostrophe escaping, our manual unescape becomes very troublesome. There seems to be no significant perf degradation Before: ``` <div> x 2,161,669 ops/sec ±7.87% (71 runs sampled) 100 x <div/> x 16,464 ops/sec ±0.71% (92 runs sampled) 100 x <FormattedNumber> x 2,421 ops/sec ±1.26% (91 runs sampled) 100 x <FormattedDate> x 903 ops/sec ±1.40% (91 runs sampled) 100 x <FormattedMessage> x 340 ops/sec ±11.89% (82 runs sampled) 100 x <FormattedMessage> with placeholder x 91.85 ops/sec ±5.76% (67 runs sampled) 100 x <FormattedMessage> with placeholder in AST form x 223 ops/sec ±3.57% (79 runs sampled) 100 x <FormattedMessage> with placeholder, cached x 505 ops/sec ±5.36% (72 runs sampled) 100 x <FormattedMessage> with placeholder, cached in AST form x 419 ops/sec ±6.04% (80 runs sampled) ``` After: ``` <div> x 2,998,677 ops/sec ±0.57% (90 runs sampled) 100 x <div/> x 15,512 ops/sec ±3.10% (87 runs sampled) 100 x <FormattedNumber> x 2,456 ops/sec ±1.51% (90 runs sampled) 100 x <FormattedDate> x 942 ops/sec ±0.99% (90 runs sampled) 100 x <FormattedMessage> x 358 ops/sec ±3.59% (82 runs sampled) 100 x <FormattedMessage> with placeholder x 103 ops/sec ±3.83% (75 runs sampled) 100 x <FormattedMessage> with placeholder in AST form x 237 ops/sec ±1.51% (82 runs sampled) 100 x <FormattedMessage> with placeholder, cached x 673 ops/sec ±2.15% (87 runs sampled) 100 x <FormattedMessage> with placeholder, cached in AST form x 485 ops/sec ±1.77% (86 runs sampled) ```
1 parent 34e99d3 commit aefb68b

File tree

2 files changed

+0
-45
lines changed

2 files changed

+0
-45
lines changed

src/formatters/message.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,8 @@ const invariant: typeof invariant_ = (invariant_ as any).default || invariant_;
1515
import {Formatters, IntlConfig, MessageDescriptor} from '../types';
1616

1717
import {createError, escape} from '../utils';
18-
import {LiteralElement, TYPE} from 'intl-messageformat-parser';
1918
import {FormatXMLElementFn, PrimitiveType} from 'intl-messageformat';
2019

21-
/**
22-
* Escape a raw msg when we run in prod mode
23-
* https://github.com/formatjs/formatjs/blob/master/packages/intl-messageformat-parser/src/parser.pegjs#L155
24-
*/
25-
function escapeUnformattedMessage(msg: string): string {
26-
return msg.replace(/'\{(.*?)\}'/g, `{$1}`);
27-
}
28-
2920
export function formatMessage(
3021
{
3122
locale,
@@ -77,21 +68,6 @@ export function formatMessage(
7768
invariant(id, '[React Intl] An `id` must be provided to format a message.');
7869

7970
const message = messages && messages[id];
80-
const hasValues = Object.keys(values).length > 0;
81-
82-
// Avoid expensive message formatting for simple messages without values. In
83-
// development messages will always be formatted in case of missing values.
84-
if (!hasValues && process.env.NODE_ENV === 'production') {
85-
const val = message || defaultMessage || id;
86-
if (typeof val === 'string') {
87-
return escapeUnformattedMessage(val);
88-
}
89-
invariant(
90-
val.length === 1 && val[0].type === TYPE.literal,
91-
'Message has placeholders but no values was provided'
92-
);
93-
return (val[0] as LiteralElement).value;
94-
}
9571

9672
let formattedMessageParts: Array<string | object> = [];
9773

test/unit/format.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,6 @@ describe('format API', () => {
722722
expect(formatMessage({id: 'ast_simple'})).toBe(mf.format());
723723
});
724724

725-
it('should throw if format AST message w/o values', () => {
726-
process.env.NODE_ENV = 'production';
727-
expect(() => formatMessage({id: 'ast_var'})).toThrow();
728-
});
729-
730725
it('formats messages with placeholders', () => {
731726
const {locale, messages} = config;
732727
const mf = new IntlMessageFormat(messages.with_arg, locale);
@@ -757,22 +752,6 @@ describe('format API', () => {
757752
);
758753
});
759754

760-
it('avoids formatting when no values and in production', () => {
761-
const {messages} = config;
762-
763-
process.env.NODE_ENV = 'production';
764-
expect(formatMessage({id: 'no_args'})).toBe(messages.no_args);
765-
expect(state.getMessageFormat).toHaveBeenCalledTimes(0);
766-
767-
const values = {foo: 'foo'};
768-
expect(formatMessage({id: 'no_args'}, values)).toBe(messages.no_args);
769-
expect(state.getMessageFormat).toHaveBeenCalledTimes(1);
770-
771-
process.env.NODE_ENV = 'development';
772-
expect(formatMessage({id: 'no_args'})).toBe(messages.no_args);
773-
expect(state.getMessageFormat).toHaveBeenCalledTimes(2);
774-
});
775-
776755
describe('fallbacks', () => {
777756
it('formats message with missing named formats', () => {
778757
const {locale, messages} = config;

0 commit comments

Comments
 (0)