aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/performanceentry/tojson/index.md
blob: 6d0277485536f8434cece5639863df38d1c7f498 (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
---
title: PerformanceEntry.toJSON()
slug: Web/API/PerformanceEntry/toJSON
tags:
  - API
  - Method
  - Méthode
  - Performance Timeline API
  - PerformanceEntry
  - Reference
  - Performance Web
translation_of: Web/API/PerformanceEntry/toJSON
---
{{APIRef("Performance Timeline API")}}

La méthode **`toJSON()`** permet de sérialiser l'objet [`PerformanceEntry`](/fr/docs/Web/API/PerformanceEntry) courant et renvoie une représentation JSON de l'objet.

{{AvailableInWorkers}}

## Syntaxe

```js
let jsonObj = perfEntry.toJSON();
```

### Arguments

Aucun.

### Valeur de retour

- `json`
  - : Un objet JSON qui est la sérialisation de l'objet [`PerformanceEntry`](/fr/docs/Web/API/PerformanceEntry).

## Exemple

L'exemple suivant montre l'utilisation de la méthode `toJSON()`.

```js
function run_PerformanceEntry() {
  console.log("Support de PerformanceEntry ...");

  if (performance.mark === undefined) {
    console.log("... performance.mark n'est pas pris en charge");
    return;
  }

  // Crée quelques entrées de performance via la méthode mark()
  performance.mark("Begin");
  do_work(50000);
  performance.mark("End");

  // Utilise getEntries() pour itérer à travers chaque entrée.
  let p = performance.getEntries();
  for (let i = 0; i < p.length; i++) {
    console.log("Entry[" + i + "]");
    check_PerformanceEntry(p[i]);
  }
}
function check_PerformanceEntry(obj) {
  let properties = ["name", "entryType", "startTime", "duration"];
  let methods = ["toJSON"];

  for (let i = 0; i < properties.length; i++) {
    // On vérifie chaque propriété
    let supported = properties[i] in obj;
    if (supported)
      console.log("..." + properties[i] + " = " + obj[properties[i]]);
    else
      console.log("..." + properties[i] + " = N'est pas pris en charge");
  }
  for (let i = 0; i < methods.length; i++) {
    // On vérifie chaque méthode
    let supported = typeof obj[methods[i]] == "function";
    if (supported) {
      let js = obj[methods[i]]();
      console.log("..." + methods[i] + "() = " + JSON.stringify(js));
    } else {
      console.log("..." + methods[i] + " = N'est pas pris en charge");
    }
  }
}
```

## Spécifications

| Spécification                                                                                                                | Statut                                                   | Commentaire                                   |
| ---------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | --------------------------------------------- |
| {{SpecName('Performance Timeline Level 2', '#dom-performanceentry-tojson',
        'toJSON')}} | {{Spec2('Performance Timeline Level 2')}} | Définition initiale de la méthode `toJSON()`. |

## Compatibilité des navigateurs

{{Compat("api.PerformanceEntry.toJSON")}}