Commit 1ca595a
committed
Merge pull request #4006 from dilijev:intl-date-toLocaleString-cache
When calling `toLocaleString` functions that are implemented in Intl like `Date.prototype.toLocaleString`, that call is essentially sugar for creating an Intl.DateTimeFormat object, and formatting `this`. We naively throw away that object on the assumption that users will cache an `Intl.DateTimeFormat` object if they plan on making the same format call repeatedly. At a minimum, we can cache the formatter object created when the calls are made with no arguments passed in.
In particular, these two code patterns are semantically identical:
```
let d = new Date();
let formatter = new Intl.DateTimeFormat();
print(formatter.format(d));
```
```
let d = new Date();
print(d.toLocaleString());
```
This change significantly improves the running time when Date.prototype.toLocaleString is called in a loop with default options. #4007 has been opened to track further-reaching improvements related to caching behavior on implicitly-created i18n objects (applicable to both WinGlob and ICU).
The remaining gap in the measurement below between ch and d8 is likely related to debug versus release and WinGlob versus ICU.
```
>eshost -h d8,ch-dev-che3,ch-master-latest caching.js
#### d8
time spent 29 ms
time spent 19 ms
time spent 18 ms
#### ch-dev-che3 # (x64_debug, with this change to add caching behavior)
time spent 106 ms
time spent 43 ms
time spent 45 ms
#### ch-master-latest # (x64_debug, baseline)
time spent 2173 ms
time spent 1676 ms
time spent 1168 ms
```
caching.js
```
const iterations = 1000;
var arr = [];
function testMethod(method) {
var start = new Date();
testDateMethod(method);
var diff = (new Date()) - start;
print('time spent ' + diff + ' ms');
}
function testDateMethod(method) {
arr = [];
var d = new Date();
for (var i = 0; i < iterations; i++) {
arr.push(method.call(d));
}
return arr.length;
}
function test()
{
testMethod(Date.prototype.toLocaleString);
testMethod(Date.prototype.toLocaleDateString);
testMethod(Date.prototype.toLocaleTimeString);
}
test();
```
No difference in runtime between calling the functions directly versus using `call`.
Additionally:
* Made some refactorings to improve readability of this code and similar code throughout Intl.js
* Fixed #4009: Intl.DateTimeFormat().format.length should be 1
* Fixed #4012: Use an enum instead of magic constants for builtin function IDs
File tree
7 files changed
+15146
-14655
lines changed- lib/Runtime/Library
- InJavascript
- test/DebuggerCommon
7 files changed
+15146
-14655
lines changedLarge diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Lines changed: 3641 additions & 3526 deletions
Large diffs are not rendered by default.
Lines changed: 3641 additions & 3526 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1703 | 1703 | | |
1704 | 1704 | | |
1705 | 1705 | | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
1706 | 1717 | | |
1707 | 1718 | | |
1708 | | - | |
| 1719 | + | |
1709 | 1720 | | |
1710 | 1721 | | |
1711 | 1722 | | |
1712 | 1723 | | |
| 1724 | + | |
1713 | 1725 | | |
1714 | | - | |
1715 | 1726 | | |
1716 | 1727 | | |
1717 | 1728 | | |
1718 | | - | |
| 1729 | + | |
| 1730 | + | |
1719 | 1731 | | |
1720 | | - | |
| 1732 | + | |
1721 | 1733 | | |
1722 | 1734 | | |
1723 | | - | |
| 1735 | + | |
1724 | 1736 | | |
1725 | 1737 | | |
1726 | | - | |
| 1738 | + | |
1727 | 1739 | | |
1728 | 1740 | | |
1729 | | - | |
| 1741 | + | |
1730 | 1742 | | |
1731 | 1743 | | |
1732 | | - | |
| 1744 | + | |
1733 | 1745 | | |
1734 | 1746 | | |
1735 | 1747 | | |
1736 | | - | |
| 1748 | + | |
1737 | 1749 | | |
1738 | 1750 | | |
1739 | 1751 | | |
1740 | | - | |
| 1752 | + | |
1741 | 1753 | | |
1742 | 1754 | | |
1743 | 1755 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
519 | 519 | | |
520 | 520 | | |
521 | 521 | | |
522 | | - | |
| 522 | + | |
523 | 523 | | |
524 | 524 | | |
525 | 525 | | |
| |||
0 commit comments