intl-format-cache
A memoizer factory for Intl format constructors.
Last updated 4 years ago by longlho .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install intl-format-cache 
SYNC missed versions from official npm registry.

Intl Format Cache

A memoizer factory for Intl format constructors.

npm Version size

Overview

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.

Usage

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"

Benchmark

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 ---

Current Tags

  • 4.3.1                                ...           latest (4 years ago)
  • 4.2.48-alpha.0                                ...           next (4 years ago)

101 Versions

  • 4.3.1                                ...           4 years ago
  • 4.3.0                                ...           4 years ago
  • 4.2.48-alpha.0                                ...           4 years ago
  • 4.2.46                                ...           4 years ago
  • 4.2.45                                ...           4 years ago
  • 4.2.44 [deprecated]           ...           4 years ago
  • 4.2.43                                ...           4 years ago
  • 4.2.42                                ...           4 years ago
  • 4.2.41                                ...           4 years ago
  • 4.2.40 [deprecated]           ...           4 years ago
  • 4.2.39                                ...           4 years ago
  • 4.2.38                                ...           4 years ago
  • 4.2.36                                ...           4 years ago
  • 4.2.35                                ...           5 years ago
  • 4.2.34                                ...           5 years ago
  • 4.2.33                                ...           5 years ago
  • 4.2.32                                ...           5 years ago
  • 4.2.31                                ...           5 years ago
  • 4.2.30                                ...           5 years ago
  • 4.2.29                                ...           5 years ago
  • 4.2.28                                ...           5 years ago
  • 4.2.27                                ...           5 years ago
  • 4.2.26                                ...           5 years ago
  • 4.2.25                                ...           5 years ago
  • 4.2.24                                ...           5 years ago
  • 4.2.23                                ...           5 years ago
  • 4.2.22                                ...           5 years ago
  • 4.2.21                                ...           5 years ago
  • 4.2.20                                ...           5 years ago
  • 4.2.19                                ...           5 years ago
  • 4.2.18                                ...           5 years ago
  • 4.2.17                                ...           5 years ago
  • 4.2.16                                ...           5 years ago
  • 4.2.15                                ...           5 years ago
  • 4.2.14                                ...           5 years ago
  • 4.2.13                                ...           5 years ago
  • 4.2.12                                ...           5 years ago
  • 4.2.11                                ...           5 years ago
  • 4.2.10                                ...           5 years ago
  • 4.2.9                                ...           5 years ago
  • 4.2.8                                ...           5 years ago
  • 4.2.8-alpha.7                                ...           5 years ago
  • 4.2.8-alpha.6                                ...           5 years ago
  • 4.2.7                                ...           5 years ago
  • 4.2.6                                ...           5 years ago
  • 4.2.5                                ...           5 years ago
  • 4.2.3                                ...           5 years ago
  • 4.2.2                                ...           5 years ago
  • 4.2.1                                ...           5 years ago
  • 4.2.0                                ...           5 years ago
  • 4.1.22                                ...           5 years ago
  • 4.1.21                                ...           5 years ago
  • 4.1.20                                ...           5 years ago
  • 4.1.19                                ...           5 years ago
  • 4.1.18                                ...           5 years ago
  • 4.1.17                                ...           5 years ago
  • 4.1.16                                ...           5 years ago
  • 4.1.15                                ...           5 years ago
  • 4.1.14                                ...           5 years ago
  • 4.1.13                                ...           5 years ago
  • 4.1.12                                ...           5 years ago
  • 4.1.11                                ...           5 years ago
  • 4.1.10                                ...           5 years ago
  • 4.1.9                                ...           5 years ago
  • 4.1.8                                ...           5 years ago
  • 4.1.7                                ...           5 years ago
  • 4.1.6                                ...           5 years ago
  • 4.1.5                                ...           5 years ago
  • 4.1.4                                ...           5 years ago
  • 4.1.3                                ...           5 years ago
  • 4.1.2                                ...           5 years ago
  • 4.1.0                                ...           5 years ago
  • 4.0.1                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.3.2                                ...           5 years ago
  • 3.3.1                                ...           5 years ago
  • 3.3.0                                ...           5 years ago
  • 3.2.1                                ...           5 years ago
  • 3.2.0                                ...           5 years ago
  • 3.1.2                                ...           5 years ago
  • 3.1.1                                ...           5 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.2                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.2.9                                ...           6 years ago
  • 2.2.7                                ...           6 years ago
  • 2.2.6                                ...           6 years ago
  • 2.2.5                                ...           6 years ago
  • 2.2.4                                ...           6 years ago
  • 2.2.2                                ...           6 years ago
  • 2.2.1                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.5                                ...           9 years ago
  • 2.0.4                                ...           10 years ago
  • 2.0.3                                ...           10 years ago
  • 2.0.2                                ...           10 years ago
  • 2.0.1                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None

Copyright 2013 - present © cnpmjs.org | Home |