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.PluralRules",
9 description: "Test the speed of the Intl.PluralRules implementation.",
15 name: "Intl.PluralRules constructor iterations",
18 { name: "Intl.PluralRules constructor accumulatedTime", unit: "ms" },
19 { name: "Intl.PluralRules constructor perCallTime", unit: "ms" },
22 name: "Intl.PluralRules.prototype.select iterations",
26 name: "Intl.PluralRules.prototype.select accumulatedTime",
30 name: "Intl.PluralRules.prototype.select perCallTime",
35 name: "Intl.PluralRules pluralCategories iterations",
39 name: "Intl.PluralRules pluralCategories accumulatedTime",
43 name: "Intl.PluralRules pluralCategories perCallTime",
50 tags: ["intl", "ecma402"],
53 add_task(function measure_pluralrules() {
54 const measureConstructor = measureIterations("Intl.PluralRules constructor");
55 const measureSelect = measureIterations("Intl.PluralRules.prototype.select");
56 const measurePluralCategories = measureIterations(
57 "Intl.PluralRules pluralCategories"
60 // Re-use the config between runs.
62 const fieldOptions = {
63 type: ["cardinal", "ordinal"],
67 function randomizeConfig(name, chance) {
68 const option = fieldOptions[name];
69 if (prng() < chance) {
70 config[name] = option[Math.floor(option.length * prng())];
76 // Split each step of the benchmark into separate JS functions so that performance
77 // profiles are easy to analyze.
79 function benchmarkPluralRulesConstructor() {
80 for (let i = 0; i < 1000; i++) {
81 // Create a random configuration powered by a pseudo-random number generator. This
82 // way the configurations will be the same between 2 different runs.
83 const locale = pickRepresentativeLocale();
84 randomizeConfig("type", 0.5);
86 // Measure the constructor.
87 measureConstructor.start();
88 const pr = new Intl.PluralRules(locale, config);
89 measureConstructor.stop();
91 benchmarkSelectOperation(pr);
92 benchmarkPluralCategories(pr);
96 function benchmarkSelectOperation(pr) {
97 // Measure the select operation.
98 for (let j = 0; j < 1000; j++) {
99 // TODO: We may want to extend this to non-integer values in the future.
100 const num = Math.floor(prng() * 10000);
101 measureSelect.start();
103 measureSelect.stop();
107 function benchmarkPluralCategories(pr) {
108 measurePluralCategories.start();
109 pr.resolvedOptions().pluralCategories;
110 measurePluralCategories.stop();
113 benchmarkPluralRulesConstructor();
114 measureConstructor.reportMetrics();
115 measureSelect.reportMetrics();
116 measurePluralCategories.reportMetrics();