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; rv:134.0) Gecko/20100101 Firefox/134.0
Browser:
Firefox 134
Operating system:
Mac OS X 10.15
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Always new instance
70425.1 Ops/sec
static global instance
3632314.2 Ops/sec
Lodash cache
1187430.0 Ops/sec
Custom cache with maps
3145074.5 Ops/sec
Custom cache with objects
2467155.5 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)