Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
intl-format-cache
Advanced tools
The intl-format-cache npm package is a utility for caching instances of Intl formatters, such as Intl.DateTimeFormat, Intl.NumberFormat, and Intl.RelativeTimeFormat. This can improve performance by reusing formatter instances instead of creating new ones each time formatting is needed.
Caching DateTimeFormat instances
This feature allows you to cache instances of Intl.DateTimeFormat. The code sample demonstrates how to create and reuse a DateTimeFormat instance for formatting dates in 'en-US' locale.
const { getDateTimeFormat } = require('intl-format-cache');
const dateTimeFormat = getDateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
console.log(dateTimeFormat.format(new Date()));
Caching NumberFormat instances
This feature allows you to cache instances of Intl.NumberFormat. The code sample demonstrates how to create and reuse a NumberFormat instance for formatting numbers as currency in 'en-US' locale.
const { getNumberFormat } = require('intl-format-cache');
const numberFormat = getNumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(numberFormat.format(123456.789));
Caching RelativeTimeFormat instances
This feature allows you to cache instances of Intl.RelativeTimeFormat. The code sample demonstrates how to create and reuse a RelativeTimeFormat instance for formatting relative time in 'en-US' locale.
const { getRelativeTimeFormat } = require('intl-format-cache');
const relativeTimeFormat = getRelativeTimeFormat('en-US');
console.log(relativeTimeFormat.format(-1, 'day'));
The intl package provides a polyfill for the ECMAScript Internationalization API, which includes Intl.DateTimeFormat, Intl.NumberFormat, and Intl.RelativeTimeFormat. Unlike intl-format-cache, it focuses on providing the functionality where it is not natively available, rather than caching instances.
The formatjs package is a collection of libraries for internationalization in JavaScript, including react-intl and intl-messageformat. It provides more comprehensive solutions for formatting dates, numbers, and messages, but does not specifically focus on caching formatter instances like intl-format-cache.
The globalize package provides a comprehensive internationalization library based on the Unicode CLDR data. It supports formatting dates, numbers, and messages, and offers more extensive localization features compared to intl-format-cache, but does not specifically focus on caching formatter instances.
A memoizer factory for Intl format constructors.
This is a helper package used within FormatJS suite. It provides a factory for creating memoizers of Intl
format constructors: Intl.NumberFormat
, Intl.DateTimeFormat
, IntlMessageFormat
, and IntlRelativeFormat
.
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.
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.
This package works as an ES6 or Node.js module, in either case it has a single default export function; e.g.:
// In an ES6 module.
import memoizeFormatConstructor from 'intl-format-cache';
// In Node.
var memoizeFormatConstructor = require('intl-format-cache');
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.
var getNumberFormat = memoizeFormatConstructor(Intl.NumberFormat);
var nf1 = getNumberFormat('en');
var nf2 = getNumberFormat('en');
var nf3 = getNumberFormat('fr');
console.log(nf1 === nf2); // => true
console.log(nf1 === nf3); // => false
console.log(nf1.format(1000)); // => "1,000"
console.log(nf3.format(1000)); // => "1 000"
fast-memoize x 19,610 ops/sec ±1.86% (73 runs sampled)
intl-format-cache x 18,854 ops/sec ±4.95% (81 runs sampled)
--- NumberFormat cache set: Fastest is fast-memoize,intl-format-cache ---
fast-memoize x 1,051,977 ops/sec ±1.53% (89 runs sampled)
intl-format-cache x 1,134,171 ops/sec ±1.19% (91 runs sampled)
not cached x 23,002 ops/sec ±2.23% (83 runs sampled)
--- NumberFormat cache get: Fastest is intl-format-cache ---
fast-memoize x 6,466 ops/sec ±6.56% (72 runs sampled)
intl-format-cache x 7,384 ops/sec ±50.43% (64 runs sampled)
--- DateTimeFormat cache set: Fastest is fast-memoize ---
fast-memoize x 965,874 ops/sec ±17.87% (90 runs sampled)
intl-format-cache x 1,048,234 ops/sec ±0.79% (89 runs sampled)
not cached x 13,543 ops/sec ±2.61% (85 runs sampled)
--- DateTimeFormat cache get: Fastest is intl-format-cache ---
fast-memoize x 72,531 ops/sec ±26.27% (79 runs sampled)
intl-format-cache x 88,729 ops/sec ±0.51% (91 runs sampled)
--- IntlMessageFormat cache set: Fastest is intl-format-cache ---
fast-memoize x 665,420 ops/sec ±2.61% (90 runs sampled)
intl-format-cache x 649,186 ops/sec ±2.19% (90 runs sampled)
not cached x 127,110 ops/sec ±0.35% (91 runs sampled)
--- IntlMessageFormat cache get: Fastest is fast-memoize ---
fast-memoize x 1,294,591 ops/sec ±1.10% (94 runs sampled)
intl-format-cache x 1,905,746 ops/sec ±0.71% (91 runs sampled)
not cached x 152,118 ops/sec ±0.47% (94 runs sampled)
--- IntlMessageFormat cache get simple arg: Fastest is intl-format-cache ---
number x 536,024 ops/sec ±0.99% (86 runs sampled)
datetime x 397,275 ops/sec ±0.92% (90 runs sampled)
messageformat x 1,278,072 ops/sec ±1.31% (89 runs sampled)
--- all formats: Fastest is messageformat ---
number x 532,863 ops/sec ±0.79% (93 runs sampled)
datetime x 377,391 ops/sec ±1.11% (89 runs sampled)
messageformat x 709,020 ops/sec ±3.19% (81 runs sampled)
--- all formats random input: Fastest is messageformat ---
FAQs
A memoizer factory for Intl format constructors.
We found that intl-format-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.