summaryrefslogtreecommitdiffstats
path: root/intl/benchmarks/perftest_dateTimeFormat.js
blob: ffb75d046408347ad5fc6a56aa5b273c046d6d70 (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
/* 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.DateTimeFormat",
  description: "Test the speed of the Intl.DateTimeFormat implementation.",
  options: {
    default: {
      perfherder: true,
      perfherder_metrics: [
        {
          name: "Intl.DateTimeFormat constructor iterations",
          unit: "iterations",
        },
        { name: "Intl.DateTimeFormat constructor accumulatedTime", unit: "ms" },
        { name: "Intl.DateTimeFormat constructor perCallTime", unit: "ms" },

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

add_task(function measure_date() {
  const measureConstructor = measureIterations(
    "Intl.DateTimeFormat constructor"
  );
  const measureFormat = measureIterations(
    "Intl.DateTimeFormat.prototype.format"
  );

  // Re-use the config between runs.

  const fieldOptions = {
    weekday: ["narrow", "short", "long"],
    era: ["narrow", "short", "long"],
    year: ["2-digit", "numeric"],
    month: ["2-digit", "numeric", "narrow", "short", "long"],
    day: ["2-digit", "numeric"],
    hour: ["2-digit", "numeric"],
    minute: ["2-digit", "numeric"],
    second: ["2-digit", "numeric"],
    timeZoneName: ["short", "long"],
  };

  const config = {};
  function randomizeConfig(name, chance) {
    const option = fieldOptions[name];
    if (prng() < chance) {
      config[name] = option[Math.floor(option.length * prng())];
    } else {
      delete config[name];
    }
  }

  let date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));

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

  function benchmarkDateTimeFormatConstructor() {
    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();
      randomizeConfig("year", 0.5);
      randomizeConfig("month", 0.5);
      randomizeConfig("day", 0.5);
      randomizeConfig("hour", 0.5);
      randomizeConfig("minute", 0.5);
      // Set the following to some lower probabilities:
      randomizeConfig("second", 0.2);
      randomizeConfig("timeZoneName", 0.2);
      randomizeConfig("weekday", 0.2);
      randomizeConfig("era", 0.1);

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

      benchmarkFormatOperation(formatter);
    }
  }

  const start = Date.UTC(2000);
  const end = Date.UTC(2030);
  const dateDiff = end - start;
  function benchmarkFormatOperation(formatter) {
    // Measure the format operation.
    for (let j = 0; j < 100; j++) {
      date = new Date(start + prng() * dateDiff);
      measureFormat.start();
      formatter.format(date);
      measureFormat.stop();
    }
  }

  benchmarkDateTimeFormatConstructor();
  measureConstructor.reportMetrics();
  measureFormat.reportMetrics();

  ok(true);
});