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/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser:
Chrome 132
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Always new instance
68960.2 Ops/sec
static global instance
2528241.0 Ops/sec
Lodash cache
1149346.6 Ops/sec
Custom cache with maps
2455811.0 Ops/sec
Custom cache with objects
2121335.2 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)