Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Memoized Intl.NumberFormat vs fresh instances
(version: 0)
Comparing performance of:
new Intl.NumberFormat vs memoized instance
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.cache = new Map(); window.format = (locale, currency, value) => new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value); window.memoizedFormat = (locale, currency, value) => { const key = [locale, currency]; let formatter = window.cache.get(key); if (!formatter) { formatter = new Intl.NumberFormat(locale, { style: 'currency', currency }); window.cache.set(key, formatter); } return formatter.format(value); };
Tests:
new Intl.NumberFormat
window.format('pt-BR', 'BRL', 100_346_999.21);
memoized instance
window.memoizedFormat('pt-BR', 'BRL', 100_346_999.21);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Intl.NumberFormat
memoized instance
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Intl.NumberFormat
42824.3 Ops/sec
memoized instance
43924.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark compares two approaches to formatting numbers in JavaScript: using a fresh instance of `Intl.NumberFormat` versus creating a memoized instance. The goal is to determine which approach provides better performance. **Options Compared** Two options are compared: 1. **Fresh Instance**: Using a new instance of `Intl.NumberFormat` every time it's needed. 2. **Memoized Instance**: Creating a single, cached instance of `Intl.NumberFormat` and reusing it for subsequent requests with the same locale and currency settings. **Pros and Cons** * **Fresh Instance**: + Pros: Easy to implement, no cache management required. + Cons: Creates a new instance every time, which can lead to performance issues due to the overhead of creating and destroying objects. * **Memoized Instance**: + Pros: Reuses the cached instance, reducing overhead and improving performance. + Cons: Requires additional memory for caching, and cache invalidation may be necessary when locale or currency settings change. **Library and Purpose** `Intl.NumberFormat` is a built-in JavaScript library that formats numbers according to the user's locale. It provides a standardized way of formatting numbers in various ways, such as currency, decimal separator, and group separator. **Special JS Feature/Syntax** None mentioned explicitly in this benchmark. However, it's worth noting that `Intl.NumberFormat` uses Unicode code points for its formatting options, which allows for support of various languages and locales. **Other Alternatives** If you're looking for alternatives to memoizing instances of `Intl.NumberFormat`, consider the following: * **LruCache**: A popular caching library for JavaScript that can be used with `Intl.NumberFormat` instances. * **WeakMap**: A built-in JavaScript data structure that can be used as a cache, allowing you to store and retrieve cached instances of `Intl.NumberFormat`. * **Custom caching solution**: Implementing your own caching mechanism using JavaScript objects or other libraries. In summary, the benchmark compares two approaches to formatting numbers in JavaScript: creating fresh instances versus memoizing instances. The memoized approach shows better performance due to reduced overhead, but requires additional memory for caching and cache invalidation.
Related benchmarks:
Number.toLocaleString vs Intl.NumberFormat memory pressure
What's faster? cached Intl.NumberFormat vs new Intl.NumberFormat vs toLocaleString currency
new Intl.NumberFormat vs cached instances
Number.toLocaleString vs Intl.NumberFormat (single instance)
Comments
Confirm delete:
Do you really want to delete benchmark?