Skip to content

Commit

Permalink
fix: remove custom unescaping of static message
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
Long Ho committed Sep 27, 2019
1 parent 34e99d3 commit aefb68b
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 45 deletions.
24 changes: 0 additions & 24 deletions src/formatters/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ const invariant: typeof invariant_ = (invariant_ as any).default || invariant_;
import {Formatters, IntlConfig, MessageDescriptor} from '../types';

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

/**
* Escape a raw msg when we run in prod mode
* https://github.com/formatjs/formatjs/blob/master/packages/intl-messageformat-parser/src/parser.pegjs#L155
*/
function escapeUnformattedMessage(msg: string): string {
return msg.replace(/'\{(.*?)\}'/g, `{$1}`);
}

export function formatMessage(
{
locale,
Expand Down Expand Up @@ -77,21 +68,6 @@ export function formatMessage(
invariant(id, '[React Intl] An `id` must be provided to format a message.');

const message = messages && messages[id];
const hasValues = Object.keys(values).length > 0;

// Avoid expensive message formatting for simple messages without values. In
// development messages will always be formatted in case of missing values.
if (!hasValues && process.env.NODE_ENV === 'production') {
const val = message || defaultMessage || id;
if (typeof val === 'string') {
return escapeUnformattedMessage(val);
}
invariant(
val.length === 1 && val[0].type === TYPE.literal,
'Message has placeholders but no values was provided'
);
return (val[0] as LiteralElement).value;
}

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

Expand Down
21 changes: 0 additions & 21 deletions test/unit/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,6 @@ describe('format API', () => {
expect(formatMessage({id: 'ast_simple'})).toBe(mf.format());
});

it('should throw if format AST message w/o values', () => {
process.env.NODE_ENV = 'production';
expect(() => formatMessage({id: 'ast_var'})).toThrow();
});

it('formats messages with placeholders', () => {
const {locale, messages} = config;
const mf = new IntlMessageFormat(messages.with_arg, locale);
Expand Down Expand Up @@ -757,22 +752,6 @@ describe('format API', () => {
);
});

it('avoids formatting when no values and in production', () => {
const {messages} = config;

process.env.NODE_ENV = 'production';
expect(formatMessage({id: 'no_args'})).toBe(messages.no_args);
expect(state.getMessageFormat).toHaveBeenCalledTimes(0);

const values = {foo: 'foo'};
expect(formatMessage({id: 'no_args'}, values)).toBe(messages.no_args);
expect(state.getMessageFormat).toHaveBeenCalledTimes(1);

process.env.NODE_ENV = 'development';
expect(formatMessage({id: 'no_args'})).toBe(messages.no_args);
expect(state.getMessageFormat).toHaveBeenCalledTimes(2);
});

describe('fallbacks', () => {
it('formats message with missing named formats', () => {
const {locale, messages} = config;
Expand Down

0 comments on commit aefb68b

Please sign in to comment.