Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Ways to cache Intl.NumberFormat
Try to test different ways of keeping Intl.NumberFormat instances for better performance.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0.1 Safari/605.1.15
Browser:
Safari 26
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
6 months ago
Test name
Executions per second
Always new instance
28431.7 Ops/sec
static global instance
1584848.4 Ops/sec
Lodash cache
1044699.2 Ops/sec
Custom cache with maps
1681499.9 Ops/sec
Custom cache with objects
1484761.0 Ops/sec
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'></script>
Script Preparation code:
const formatOptions = { minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: false, style: 'percent', }; const staticInstanceDe = new Intl.NumberFormat("de", formatOptions); const staticInstanceFr = new Intl.NumberFormat("fr", formatOptions); const lodashCache = _.memoize( (locale, options) => new Intl.NumberFormat(locale, options), (locale, options) => `${locale}-${JSON.stringify(options)}`, ); const mapCache = new Map(); const getMapCacheInstance = (locale, options) => { let lang; if (!mapCache.has(locale)) { lang = new Map(); mapCache.set(locale, lang); } else { lang = mapCache.get(locale); } if (!lang.has(options)) { const newInstance = new Intl.NumberFormat(locale, options); lang.set(options, newInstance); return newInstance; } return lang.get(options); } const objCache = {}; const getObjCacheInstance = (locale, options) => { let lang = objCache[locale]; if (!lang) { lang = {}; objCache[locale] = lang; } if (!lang[options]) { const newInstance = new Intl.NumberFormat(locale, options); lang[options] = newInstance; return newInstance; } return lang[options]; }
Tests:
Always new instance
new Intl.NumberFormat("de", formatOptions).format(0.123) new Intl.NumberFormat("fr", formatOptions).format(0.123)
static global instance
staticInstanceDe.format(0.123) staticInstanceFr.format(0.123)
Lodash cache
lodashCache("de", formatOptions).format(0.123) lodashCache("fr", formatOptions).format(0.123)
Custom cache with maps
getMapCacheInstance("de", formatOptions).format(0.123) getMapCacheInstance("fr", formatOptions).format(0.123)
Custom cache with objects
getObjCacheInstance("de", formatOptions).format(0.123) getObjCacheInstance("fr", formatOptions).format(0.123)