Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Intl.NumberFormat
(version: 0)
Comparing performance of:
Re-creating formatter vs Caching formatter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var numbers = [] for(i=0; i<10000; i++){ numbers.push = Math.random() * 10000; }
Tests:
Re-creating formatter
for (let i = 0; i < numbers.length; i++) { new Intl.NumberFormat('en').format(numbers[i]) }
Caching formatter
const formatter = new Intl.NumberFormat('en') for (let i = 0; i < numbers.length; i++) { formatter.format(numbers[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Re-creating formatter
Caching formatter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Re-creating formatter
27701328.0 Ops/sec
Caching formatter
229532.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is testing the performance of two approaches for formatting numbers using the `Intl.NumberFormat` API in JavaScript. The script preparation code generates an array of 10,000 random numbers between 0 and 10,000. There are no specific library or special JS features mentioned in this definition. The focus is on comparing the performance of re-creating a formatter instance vs caching it. **Test Cases** There are two test cases: 1. **Re-creating formatter**: This test case measures the time it takes to create a new `Intl.NumberFormat` instance for each iteration. 2. **Caching formatter**: This test case measures the time it takes to cache the first formatter instance and reuse it for all iterations. **Options Compared** The benchmark is comparing two options: * Re-creating a new formatter instance on each iteration (`new Intl.NumberFormat('en').format(numbers[i])`). * Caching the first formatter instance and reusing it for all iterations (`const formatter = new Intl.NumberFormat('en'); for (let i = 0; i < numbers.length; i++) { formatter.format(numbers[i]) }`). **Pros and Cons of Each Approach** 1. **Re-creating formatter on each iteration**: * Pros: Easy to implement, no memory overhead. * Cons: Creates a new instance on every iteration, which can lead to performance issues due to the overhead of creating a new object. 2. **Caching formatter**: * Pros: Reduces the overhead of creating a new instance, as it only happens once. * Cons: Requires more memory to store the cached instance. **Library and Purpose** The `Intl.NumberFormat` API is a built-in JavaScript API for formatting numbers according to the conventions of different languages and regions. In this benchmark, it's used to format numbers with the English locale (`'en'`). **Special JS Feature or Syntax** There are no special JS features or syntax mentioned in this benchmark. **Alternatives** Other alternatives to caching formatter instances could include: * Creating a single instance of `Intl.NumberFormat` at the start of the script and reusing it throughout the execution. * Using a caching library like LRU Cache or Cache.js to implement caching for the formatter instance. Keep in mind that these alternatives might introduce additional overhead or complexity, but they can provide better performance and memory efficiency.
Related benchmarks:
String looping test
The Non Repeating Number
For Loops Teflora Test
For Loops Teflora Test 2
Comments
Confirm delete:
Do you really want to delete benchmark?