@httpx/memo-intl
v1.1.3
Published
LRU-based memoizer for Intl constructors.
Downloads
209
Readme
@httpx/memo-intl
LRU-based memoizer for Intl.NumberFormat and Intl.DateFormat constructors.
Install
$ npm install @httpx/memo-intl
$ yarn add @httpx/memo-intl
$ pnpm add @httpx/memo-intl
Features
- 👉 Don't re-create the same Intl instance for the same options (memoized).
- 👉 Keep the Intl api untouched. Just replace
new Intl.NumberFormat
byMIntl.NumberFormat
... - 👉 Up to 20x faster than non-memoized Intl constructors.
- 👉 Decrease memory usage, unwanted memory leaks and garbage collection pressure.
- 👉 Max out 50 cache instances by default with quick-lru.
- 👉 Lightweight. Node, browser and edge support.
Documentation
👉 Official website or Github Readme
Usage
MIntl.NumberFormat
import { MIntl } from '@httpx/memo-intl';
// Notice: `new Intl.NumberFormat` vs `MIntl.NumberFormat`
const formattedPrice = MIntl.NumberFormat('fr-FR', {
style: 'currency',
currency: 'EUR',
notation: 'compact',
minimumFractionDigits: 2,
}).format(row.price);
MIntl.DateTimeFormat
import { MIntl } from '@httpx/memo-intl';
// Notice: `new Intl.DateTimeFormat` vs `MIntl.DateTimeFormat
const formatter = MIntl.DateTimeFormat('fr-FR', {
dateStyle: 'full',
timeStyle: 'full',
timeZone: 'UTC'
});
const date = Date.parse('2024-05-29T07:42:43.230Z');
expect(formatter.format(date)).toBe(
'mercredi 29 mai 2024 à 07:42:43 temps universel coordonné'
);
expectTypeOf(formatter).toEqualTypeOf<Intl.DateTimeFormat>();
Benchmarks
Performance is monitored with codspeed.io.
IntlNumberFormat(locale, options) x 10_000
, see bench for details.
RUN v1.6.0 /home/sebastien/github/httpx/packages/memo-intl
✓ bench/m-intl.bench.ts (2) 3626ms
✓ MIntl benchmarks (2) 3624ms
name hz min max mean p75 p99 p995 p999 rme samples
· With memoization `MIntl.NumberFormat()` 108.24 8.6995 10.5676 9.2388 9.8779 10.5676 10.5676 10.5676 ±5.22% 10 fastest
· Without memoization `new Intl.NumberFormat()` 5.0021 192.58 210.78 199.92 201.81 210.78 210.78 210.78 ±1.81% 10
BENCH Summary
With memoization `MIntl.NumberFormatter()` - bench/m-intl.bench.ts > MIntl benchmarks
21.64x faster than Without memoization `new Intl.NumberFormatter()`
Bundle size
Bundle size is tracked by a size-limit configuration
| Scenario | Size with deps (compressed) | |------------------------------------------------|----------------------------:| | `import { MIntl } from '@httpx/memo-intl' | ~ 1.2kB |
Note that per-se the library weigths less than 300 bytes, the quick-lru dependency makes the difference. For CJS usage (not recommended) track the size on bundlephobia.
Compatibility
| Level | CI | Description |
|--------------|----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Node | ✅ | CI for 18.x, 20.x & 22.x. |
| Browser | ✅ | Tested with latest chrome (vitest/playwright) |
| Browserslist | ✅ | > 96% on 12/2023. Mins to Chrome 96+, Firefox 90+, Edge 19+, iOS 12+, Safari 12+, Opera 77+ |
| Edge | ✅ | Ensured on CI with @vercel/edge-runtime. |
| Cloudflare | ✅ | Ensured with @cloudflare/vitest-pool-workers (see wrangler.toml |
| Typescript | ✅ | TS 5.0 + / are-the-type-wrong checks on CI. |
| ES2022 | ✅ | Dist files checked with es-check |
| Performance| ✅ | Monitored with with codspeed.io |
For older browsers: most frontend frameworks can transpile the library (ie: nextjs...)
Contributors
Contributions are warmly appreciated. Have a look to the CONTRIBUTING document.
Sponsors
If my OSS work brightens your day, let's take it to new heights together! Sponsor, coffee, or star – any gesture of support fuels my passion to improve. Thanks for being awesome! 🙏❤️
Special thanks to
License
MIT © belgattitude and contributors.