aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/json/json_perf.html
blob: 0c79b454fa5ed2c0b1b0b94b80acc31ec444a56c (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
<!DOCTYPE html>
<html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Performance Tests - goog.json vs JSON</title>
  <link rel="stylesheet" type="text/css" href="../testing/performancetable.css"/>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.json');
    goog.require('goog.string');
    goog.require('goog.testing.PerformanceTable');
    goog.require('goog.testing.PropertyReplacer');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
<h1>goog.json and JSON Performance Tests</h1>
<p>
<strong>User-agent:</strong>
<script>document.write(navigator.userAgent);</script>
</p>
<div id="perfTable"></div>
<hr>

<script>
var table = new goog.testing.PerformanceTable(
    goog.dom.getElement('perfTable'));

var stubs = new goog.testing.PropertyReplacer();

function tearDown() {
  stubs.reset();
}

function testSerialize() {
  var obj = populateObject({}, 50, 4);

  table.run(function() {
    var s = JSON.stringify(obj);
  }, 'Stringify using JSON.stringify');

  table.run(function() {
    var s = goog.json.serialize(obj);
  }, 'Stringify using goog.json.serialize');
}

function testParse() {
  var obj = populateObject({}, 50, 4);
  var s = JSON.stringify(obj);

  table.run(function() {
    var o = JSON.parse(s);
  }, 'Parse using JSON.parse');

  table.run(function() {
    var o = goog.json.parse(s);
  }, 'Parse using goog.json.parse');

  table.run(function() {
    var o = goog.json.unsafeParse(s);
  }, 'Parse using goog.json.unsafeParse');
}

/**
 * @param obj The object to add properties to.
 * @param numProperties The number of properties to add.
 * @param depth The depth at which to recursively add properties.
 */
function populateObject(obj, numProperties, depth) {
  if (depth == 0) {
    return randomLiteral();
  }

  // Make an object with a mix of strings, numbers, arrays, objects, booleans
  // nulls as children.
  for (var i = 0; i < numProperties; i++) {
    var bucket = goog.math.randomInt(3);
    switch (bucket) {
      case 0:
        obj[i] = randomLiteral();
        break;
      case 1:
        obj[i] = populateObject({}, numProperties, depth - 1);
        break;
      case 2:
        obj[i] = populateObject([], numProperties, depth - 1);
        break;
    }
  }
  return obj;
}

function randomLiteral() {
  var bucket = goog.math.randomInt(3);
  switch (bucket) {
    case 0:
      return goog.string.getRandomString();
    case 1:
      return Math.random();
    case 2:
      return Math.random() >= .5;
  }
  return null;
}

</script>
</body>
</html>