View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.performance;
16  
17  import org.htmlunit.corejs.javascript.Scriptable;
18  import org.htmlunit.javascript.JavaScriptEngine;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxFunction;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  import org.htmlunit.javascript.host.event.EventTarget;
24  
25  /**
26   * JavaScript host object for {@code Performance}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   *
31   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance">MDN Documentation</a>
32   */
33  @JsxClass
34  public class Performance extends EventTarget {
35      private PerformanceTiming timing_;
36  
37      /**
38       * Creates an instance of this object.
39       */
40      @Override
41      @JsxConstructor
42      public void jsConstructor() {
43          super.jsConstructor();
44      }
45  
46      /**
47       * Returns the {@code navigation} property.
48       *
49       * @return the {@code navigation} property
50       */
51      @JsxGetter
52      public PerformanceNavigation getNavigation() {
53          final PerformanceNavigation navigation = new PerformanceNavigation();
54          navigation.setParentScope(getParentScope());
55          navigation.setPrototype(getPrototype(navigation.getClass()));
56          return navigation;
57      }
58  
59      /**
60       * Returns the {@code timing} property.
61       *
62       * @return the {@code timing} property
63       */
64      @JsxGetter
65      public PerformanceTiming getTiming() {
66          if (timing_ == null) {
67              final PerformanceTiming timing = new PerformanceTiming();
68              timing.setParentScope(getParentScope());
69              timing.setPrototype(getPrototype(timing.getClass()));
70              timing_ = timing;
71          }
72  
73          return timing_;
74      }
75  
76      /**
77       * Returns a high-resolution timestamp representing the time elapsed since the time origin.
78       *
79       * @return a timestamp in milliseconds
80       */
81      @JsxFunction
82      public double now() {
83          return System.nanoTime() / 1_000_000d;
84      }
85  
86      /**
87       * Returns a list of all {@link PerformanceEntry} objects for the page.
88       * The entries can be created by making performance marks or measures (for example
89       * by calling the {@code mark()} method) at explicit points in time.
90       * If you are only interested in performance entries of certain types or that have
91       * certain names, see {@code getEntriesByType()} and {@code getEntriesByName()}.
92       *
93       * @return a list of all {@link PerformanceEntry} objects
94       */
95      @JsxFunction
96      public Scriptable getEntries() {
97          return JavaScriptEngine.newArray(getParentScope(), 0);
98      }
99  
100     /**
101      * Returns a list of all {@link PerformanceEntry} objects for the page with the given name.
102      * The entries can be created by making performance marks or measures (for example
103      * by calling the {@code mark()} method) at explicit points in time.
104      * If you are only interested in performance entries of certain types, see
105      * {@code getEntriesByType()}.
106      *
107      * @return a list of {@link PerformanceEntry} objects matching the given name
108      */
109     @JsxFunction
110     public Scriptable getEntriesByName() {
111         return JavaScriptEngine.newArray(getParentScope(), 0);
112     }
113 
114     /**
115      * Returns a list of {@link PerformanceEntry} objects of the given type.
116      * The entries can be created by making performance marks or measures
117      * (for example by calling the {@code mark()} method) at explicit points in time.
118      *
119      * @return a list of {@link PerformanceEntry} objects of the given type
120      */
121     @JsxFunction
122     public Scriptable getEntriesByType() {
123         return JavaScriptEngine.newArray(getParentScope(), 0);
124     }
125 }