1 | # Intl Format Cache
|
2 |
|
3 | A memoizer factory for Intl format constructors.
|
4 |
|
5 | [![npm Version][npm-badge]][npm]
|
6 | ![size](https://badgen.net/bundlephobia/minzip/intl-format-cache)
|
7 |
|
8 | ## Overview
|
9 |
|
10 | This is a helper package used within [FormatJS suite][formatjs]. It provides a factory for creating memoizers of [`Intl`][intl] format constructors: [`Intl.NumberFormat`][intl-nf], [`Intl.DateTimeFormat`][intl-dtf], [`IntlMessageFormat`][intl-mf], and [`IntlRelativeFormat`][intl-rf].
|
11 |
|
12 | Creating instances of these `Intl` formats is an expensive operation, and the APIs are designed such that developers should re-use format instances instead of always creating new ones. This package is simply to make it easier to create a cache of format instances of a particular type to aid in their reuse.
|
13 |
|
14 | Under the hood, this package creates a cache key based on the arguments passed to the memoized constructor (it will even order the keys of the `options` argument) it uses `JSON.stringify()` to create the string key.
|
15 |
|
16 | ## Usage
|
17 |
|
18 | This package works as an ES6 or Node.js module, in either case it has a single default export function; e.g.:
|
19 |
|
20 | ```js
|
21 | // In an ES6 module.
|
22 | import memoizeFormatConstructor from 'intl-format-cache';
|
23 | ```
|
24 |
|
25 | ```js
|
26 | // In Node.
|
27 | var memoizeFormatConstructor = require('intl-format-cache');
|
28 | ```
|
29 |
|
30 | This default export is a factory function which can be passed an `Intl` format constructor and it will return a memoizer that will create or reuse an `Intl` format instance and return it.
|
31 |
|
32 | ```js
|
33 | var getNumberFormat = memoizeFormatConstructor(Intl.NumberFormat);
|
34 |
|
35 | var nf1 = getNumberFormat('en');
|
36 | var nf2 = getNumberFormat('en');
|
37 | var nf3 = getNumberFormat('fr');
|
38 |
|
39 | console.log(nf1 === nf2); // => true
|
40 | console.log(nf1 === nf3); // => false
|
41 |
|
42 | console.log(nf1.format(1000)); // => "1,000"
|
43 | console.log(nf3.format(1000)); // => "1 000"
|
44 | ```
|
45 |
|
46 | # Benchmark
|
47 |
|
48 | ```
|
49 | fast-memoize x 19,610 ops/sec ±1.86% (73 runs sampled)
|
50 | intl-format-cache x 18,854 ops/sec ±4.95% (81 runs sampled)
|
51 | --- NumberFormat cache set: Fastest is fast-memoize,intl-format-cache ---
|
52 |
|
53 | fast-memoize x 1,051,977 ops/sec ±1.53% (89 runs sampled)
|
54 | intl-format-cache x 1,134,171 ops/sec ±1.19% (91 runs sampled)
|
55 | not cached x 23,002 ops/sec ±2.23% (83 runs sampled)
|
56 | --- NumberFormat cache get: Fastest is intl-format-cache ---
|
57 |
|
58 | fast-memoize x 6,466 ops/sec ±6.56% (72 runs sampled)
|
59 | intl-format-cache x 7,384 ops/sec ±50.43% (64 runs sampled)
|
60 | --- DateTimeFormat cache set: Fastest is fast-memoize ---
|
61 |
|
62 | fast-memoize x 965,874 ops/sec ±17.87% (90 runs sampled)
|
63 | intl-format-cache x 1,048,234 ops/sec ±0.79% (89 runs sampled)
|
64 | not cached x 13,543 ops/sec ±2.61% (85 runs sampled)
|
65 | --- DateTimeFormat cache get: Fastest is intl-format-cache ---
|
66 |
|
67 | fast-memoize x 72,531 ops/sec ±26.27% (79 runs sampled)
|
68 | intl-format-cache x 88,729 ops/sec ±0.51% (91 runs sampled)
|
69 | --- IntlMessageFormat cache set: Fastest is intl-format-cache ---
|
70 |
|
71 | fast-memoize x 665,420 ops/sec ±2.61% (90 runs sampled)
|
72 | intl-format-cache x 649,186 ops/sec ±2.19% (90 runs sampled)
|
73 | not cached x 127,110 ops/sec ±0.35% (91 runs sampled)
|
74 | --- IntlMessageFormat cache get: Fastest is fast-memoize ---
|
75 |
|
76 | fast-memoize x 1,294,591 ops/sec ±1.10% (94 runs sampled)
|
77 | intl-format-cache x 1,905,746 ops/sec ±0.71% (91 runs sampled)
|
78 | not cached x 152,118 ops/sec ±0.47% (94 runs sampled)
|
79 | --- IntlMessageFormat cache get simple arg: Fastest is intl-format-cache ---
|
80 |
|
81 | number x 536,024 ops/sec ±0.99% (86 runs sampled)
|
82 | datetime x 397,275 ops/sec ±0.92% (90 runs sampled)
|
83 | messageformat x 1,278,072 ops/sec ±1.31% (89 runs sampled)
|
84 | --- all formats: Fastest is messageformat ---
|
85 |
|
86 | number x 532,863 ops/sec ±0.79% (93 runs sampled)
|
87 | datetime x 377,391 ops/sec ±1.11% (89 runs sampled)
|
88 | messageformat x 709,020 ops/sec ±3.19% (81 runs sampled)
|
89 | --- all formats random input: Fastest is messageformat ---
|
90 | ```
|
91 |
|
92 | [npm]: https://www.npmjs.org/package/intl-format-cache
|
93 | [npm-badge]: https://img.shields.io/npm/v/intl-format-cache.svg?style=flat-square
|
94 | [intl]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
|
95 | [intl-nf]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
|
96 | [intl-dtf]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
|
97 | [intl-mf]: https://github.com/formatjs/formatjs
|
98 | [intl-rf]: https://github.com/formatjs/formatjs
|
99 | [formatjs]: http://formatjs.io/
|