---
title: PerformanceEntry.toJSON()
slug: Web/API/PerformanceEntry/toJSON
tags:
  - PerformanceEntry.toJSON()
translation_of: Web/API/PerformanceEntry/toJSON
---
<div>{{APIRef("Performance Timeline API")}}</div>

<p><strong><code>toJSON()</code></strong> 方法是一个串行器( <em>serializer </em>); 它返回{{domxref("PerformanceEntry","performance entry")}}对象的一个JSON表示形式。</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">const json = perfEntry.toJSON();
</pre>

<h3 id="Arguments">Arguments</h3>

<dl>
 <dt>None</dt>
 <dd> </dd>
</dl>

<h3 id="Return_value">Return value</h3>

<dl>
 <dt>json</dt>
 <dd>A JSON object that is the serialization of the {{domxref("PerformanceEntry")}} object.</dd>
</dl>

<h2 id="Example">Example</h2>

<p>The following example shows the use of the <code>toJSON()</code> method.</p>

<pre class="brush: js">function run_PerformanceEntry() {
  log("PerformanceEntry support ...");

  if (performance.mark === undefined) {
    log("... performance.mark Not supported");
    return;
  }

  // Create some performance entries via the mark() method
  performance.mark("Begin");
  do_work(50000);
  performance.mark("End");

  // Use getEntries() to iterate through the each entry
  var p = performance.getEntries();
  for (var i=0; i &lt; p.length; i++) {
    log("Entry[" + i + "]");
    check_PerformanceEntry(p[i]);
  }
}
function check_PerformanceEntry(obj) {
  var properties = ["name", "entryType", "startTime", "duration"];
  var methods = ["toJSON"];

  for (var i=0; i &lt; properties.length; i++) {
    // check each property
    var supported = properties[i] in obj;
    if (supported)
      log("..." + properties[i] + " = " + obj[properties[i]]);
    else
      log("..." + properties[i] + " = Not supported");
  }
  for (var i=0; i &lt; methods.length; i++) {
    // check each method
    var supported = typeof obj[methods[i]] == "function";
    if (supported) {
      var js = obj[methods[i]]();
      log("..." + methods[i] + "() = " + JSON.stringify(js));
    } else {
      log("..." + methods[i] + " = Not supported");
    }
  }
}
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Performance Timeline Level 2', '#dom-performanceentry', 'toJSON')}}</td>
   <td>{{Spec2('Performance Timeline Level 2')}}</td>
   <td>Initial definition of <code>toJSON()</code> method.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>
<div>


<p>{{Compat("api.PerformanceEntry.toJSON")}}</p>
</div>
</div>