summaryrefslogtreecommitdiffstats
path: root/intl/benchmarks/perftest_numberFormat.js
blob: c2a254c6ab417aaa70849933f793aa5ac4851a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @ts-check

var perfMetadata = {
  owner: "Internationalization Team",
  name: "Intl.NumberFormat",
  description: "Test the speed of the Intl.NumberFormat implementation.",
  options: {
    default: {
      perfherder: true,
      perfherder_metrics: [
        {
          name: "Intl.NumberFormat constructor iterations",
          unit: "iterations",
        },
        { name: "Intl.NumberFormat constructor accumulatedTime", unit: "ms" },
        { name: "Intl.NumberFormat constructor perCallTime", unit: "ms" },

        {
          name: "Intl.NumberFormat.prototype.format iterations",
          unit: "iterations",
        },
        {
          name: "Intl.NumberFormat.prototype.format accumulatedTime",
          unit: "ms",
        },
        {
          name: "Intl.NumberFormat.prototype.format perCallTime",
          unit: "ms",
        },

        {
          name: "Intl.NumberFormat.prototype.formatToParts iterations",
          unit: "iterations",
        },
        {
          name: "Intl.NumberFormat.prototype.formatToParts accumulatedTime",
          unit: "ms",
        },
        {
          name: "Intl.NumberFormat.prototype.formatToParts perCallTime",
          unit: "ms",
        },
      ],
      verbose: true,
    },
  },
  tags: ["intl", "ecma402"],
};

add_task(function measure_numberformat() {
  const measureConstructor = measureIterations("Intl.NumberFormat constructor");
  const measureFormat = measureIterations("Intl.NumberFormat.prototype.format");
  const measureFormatToParts = measureIterations(
    "Intl.NumberFormat.prototype.formatToParts"
  );

  // Re-use the config between runs.

  const styles = ["decimal", "percent", "currency", "unit"];

  const numberStyles = [
    "arab",
    "arabext",
    "bali",
    "beng",
    "deva",
    "fullwide",
    "gujr",
    "guru",
    "hanidec",
    "khmr",
    "knda",
    "laoo",
    "latn",
    "limb",
    "mlym",
    "mong",
    "mymr",
    "orya",
    "tamldec",
    "telu",
    "thai",
    "tibt",
  ];

  const decimalOptions = {
    notation: ["scientific", "engineering", "compact"],
    useGroup: [true, false],
  };

  const currencyOptions = {
    currency: ["USD", "CAD", "EUR", "Yen", "MXN", "SAR", "INR", "CNY", "IDR"],
    currencyDisplay: ["symbol", "narrowSymbol", "code", "name"],
    currencySign: ["accounting", "standard"],
  };

  const unitOptions = {
    unit: [
      "acre",
      "bit",
      "byte",
      "celsius",
      "centimeter",
      "day",
      "degree",
      "fahrenheit",
      "fluid-ounce",
      "foot",
      "gallon",
      "gigabit",
      "gigabyte",
      "gram",
      "hectare",
      "hour",
      "inch",
      "kilobit",
      "kilobyte",
      "kilogram",
      "kilometer",
      "liter",
      "megabit",
      "megabyte",
      "meter",
      "mile",
      "mile-scandinavian",
      "milliliter",
      "millimeter",
      "millisecond",
      "minute",
      "month",
      "ounce",
      "percent",
      "petabyte",
      "pound",
      "second",
      "stone",
      "terabit",
      "terabyte",
      "week",
      "yard",
      "year",
      "meter-per-second",
      "kilometer-per-hour",
    ],
    unitDisplay: ["long", "short", "narrow"],
  };

  function choose(options) {
    return options[Math.floor(options.length * prng())];
  }

  function randomizeConfig(config, options) {
    for (let option in options) {
      config[option] = choose(options[option]);
    }
  }

  // Split each step of the benchmark into separate JS functions so that performance
  // profiles are easy to analyze.

  function benchmarkNumberFormatConstructor() {
    for (let i = 0; i < 1000; i++) {
      // Create a random configuration powered by a pseudo-random number generator. This
      // way the configurations will be the same between 2 different runs.
      const locale = pickRepresentativeLocale();
      const style = choose(styles);
      const nu = choose(numberStyles);
      let config = {
        style,
        nu,
      };
      if (style == "decimal") {
        randomizeConfig(config, decimalOptions);
      } else if (style == "currency") {
        randomizeConfig(config, currencyOptions);
      } else if (style == "unit") {
        randomizeConfig(config, unitOptions);
      }

      // Measure the constructor.
      measureConstructor.start();
      const formatter = Intl.NumberFormat(locale, config);
      // Also include one format operation to ensure the constructor is de-lazified.
      formatter.format(0);
      measureConstructor.stop();

      benchmarkFormatOperation(formatter);
      benchmarkFormatToPartsOperation(formatter);
    }
  }

  function benchmarkFormatOperation(formatter) {
    // Measure the format operation.
    for (let j = 0; j < 100; j++) {
      let num = -1e6 + prng() * 2e6;
      measureFormat.start();
      formatter.format(num);
      measureFormat.stop();
    }
  }

  function benchmarkFormatToPartsOperation(formatter) {
    // Measure the formatToParts operation.
    for (let j = 0; j < 100; j++) {
      let num = -1e6 + prng() * 2e6;
      measureFormatToParts.start();
      formatter.formatToParts(num);
      measureFormatToParts.stop();
    }
  }

  benchmarkNumberFormatConstructor();
  measureConstructor.reportMetrics();
  measureFormat.reportMetrics();
  measureFormatToParts.reportMetrics();

  ok(true);
});