1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 owner: "Internationalization Team",
8 name: "Intl.DateTimeFormat",
9 description: "Test the speed of the Intl.DateTimeFormat implementation.",
15 name: "Intl.DateTimeFormat constructor iterations",
18 { name: "Intl.DateTimeFormat constructor accumulatedTime", unit: "ms" },
19 { name: "Intl.DateTimeFormat constructor perCallTime", unit: "ms" },
22 name: "Intl.DateTimeFormat.prototype.format iterations",
26 name: "Intl.DateTimeFormat.prototype.format accumulatedTime",
30 name: "Intl.DateTimeFormat.prototype.format perCallTime",
37 tags: ["intl", "ecma402"],
40 add_task(function measure_date() {
41 const measureConstructor = measureIterations(
42 "Intl.DateTimeFormat constructor"
44 const measureFormat = measureIterations(
45 "Intl.DateTimeFormat.prototype.format"
48 // Re-use the config between runs.
50 const fieldOptions = {
51 weekday: ["narrow", "short", "long"],
52 era: ["narrow", "short", "long"],
53 year: ["2-digit", "numeric"],
54 month: ["2-digit", "numeric", "narrow", "short", "long"],
55 day: ["2-digit", "numeric"],
56 hour: ["2-digit", "numeric"],
57 minute: ["2-digit", "numeric"],
58 second: ["2-digit", "numeric"],
59 timeZoneName: ["short", "long"],
63 function randomizeConfig(name, chance) {
64 const option = fieldOptions[name];
65 if (prng() < chance) {
66 config[name] = option[Math.floor(option.length * prng())];
72 let date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));
74 // Split each step of the benchmark into separate JS functions so that performance
75 // profiles are easy to analyze.
77 function benchmarkDateTimeFormatConstructor() {
78 for (let i = 0; i < 1000; i++) {
79 // Create a random configuration powered by a pseudo-random number generator. This
80 // way the configurations will be the same between 2 different runs.
81 const locale = pickRepresentativeLocale();
82 randomizeConfig("year", 0.5);
83 randomizeConfig("month", 0.5);
84 randomizeConfig("day", 0.5);
85 randomizeConfig("hour", 0.5);
86 randomizeConfig("minute", 0.5);
87 // Set the following to some lower probabilities:
88 randomizeConfig("second", 0.2);
89 randomizeConfig("timeZoneName", 0.2);
90 randomizeConfig("weekday", 0.2);
91 randomizeConfig("era", 0.1);
93 // Measure the constructor.
94 measureConstructor.start();
95 const formatter = Intl.DateTimeFormat(locale, config);
96 // Also include one format operation to ensure the constructor is de-lazified.
97 formatter.format(date);
98 measureConstructor.stop();
100 benchmarkFormatOperation(formatter);
104 const start = Date.UTC(2000);
105 const end = Date.UTC(2030);
106 const dateDiff = end - start;
107 function benchmarkFormatOperation(formatter) {
108 // Measure the format operation.
109 for (let j = 0; j < 100; j++) {
110 date = new Date(start + prng() * dateDiff);
111 measureFormat.start();
112 formatter.format(date);
113 measureFormat.stop();
117 benchmarkDateTimeFormatConstructor();
118 measureConstructor.reportMetrics();
119 measureFormat.reportMetrics();