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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Browser:
Chrome 134
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Always new instance
22599.6 Ops/sec
static global instance
853766.8 Ops/sec
Lodash cache
505681.2 Ops/sec
Custom cache with maps
811703.8 Ops/sec
Custom cache with objects
707904.9 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)