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.NumberFormat",
9 description: "Test the speed of the Intl.NumberFormat implementation.",
15 name: "Intl.NumberFormat constructor iterations",
18 { name: "Intl.NumberFormat constructor accumulatedTime", unit: "ms" },
19 { name: "Intl.NumberFormat constructor perCallTime", unit: "ms" },
22 name: "Intl.NumberFormat.prototype.format iterations",
26 name: "Intl.NumberFormat.prototype.format accumulatedTime",
30 name: "Intl.NumberFormat.prototype.format perCallTime",
35 name: "Intl.NumberFormat.prototype.formatToParts iterations",
39 name: "Intl.NumberFormat.prototype.formatToParts accumulatedTime",
43 name: "Intl.NumberFormat.prototype.formatToParts perCallTime",
50 tags: ["intl", "ecma402"],
53 add_task(function measure_numberformat() {
54 const measureConstructor = measureIterations("Intl.NumberFormat constructor");
55 const measureFormat = measureIterations("Intl.NumberFormat.prototype.format");
56 const measureFormatToParts = measureIterations(
57 "Intl.NumberFormat.prototype.formatToParts"
60 // Re-use the config between runs.
62 const styles = ["decimal", "percent", "currency", "unit"];
64 const numberStyles = [
89 const decimalOptions = {
90 notation: ["scientific", "engineering", "compact"],
91 useGroup: [true, false],
94 const currencyOptions = {
95 currency: ["USD", "CAD", "EUR", "Yen", "MXN", "SAR", "INR", "CNY", "IDR"],
96 currencyDisplay: ["symbol", "narrowSymbol", "code", "name"],
97 currencySign: ["accounting", "standard"],
100 const unitOptions = {
146 "kilometer-per-hour",
148 unitDisplay: ["long", "short", "narrow"],
151 function choose(options) {
152 return options[Math.floor(options.length * prng())];
155 function randomizeConfig(config, options) {
156 for (let option in options) {
157 config[option] = choose(options[option]);
161 // Split each step of the benchmark into separate JS functions so that performance
162 // profiles are easy to analyze.
164 function benchmarkNumberFormatConstructor() {
165 for (let i = 0; i < 1000; i++) {
166 // Create a random configuration powered by a pseudo-random number generator. This
167 // way the configurations will be the same between 2 different runs.
168 const locale = pickRepresentativeLocale();
169 const style = choose(styles);
170 const nu = choose(numberStyles);
175 if (style == "decimal") {
176 randomizeConfig(config, decimalOptions);
177 } else if (style == "currency") {
178 randomizeConfig(config, currencyOptions);
179 } else if (style == "unit") {
180 randomizeConfig(config, unitOptions);
183 // Measure the constructor.
184 measureConstructor.start();
185 const formatter = Intl.NumberFormat(locale, config);
186 // Also include one format operation to ensure the constructor is de-lazified.
188 measureConstructor.stop();
190 benchmarkFormatOperation(formatter);
191 benchmarkFormatToPartsOperation(formatter);
195 function benchmarkFormatOperation(formatter) {
196 // Measure the format operation.
197 for (let j = 0; j < 100; j++) {
198 let num = -1e6 + prng() * 2e6;
199 measureFormat.start();
200 formatter.format(num);
201 measureFormat.stop();
205 function benchmarkFormatToPartsOperation(formatter) {
206 // Measure the formatToParts operation.
207 for (let j = 0; j < 100; j++) {
208 let num = -1e6 + prng() * 2e6;
209 measureFormatToParts.start();
210 formatter.formatToParts(num);
211 measureFormatToParts.stop();
215 benchmarkNumberFormatConstructor();
216 measureConstructor.reportMetrics();
217 measureFormat.reportMetrics();
218 measureFormatToParts.reportMetrics();