Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/json/json_perf.js
2868 views
1
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://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
/**
16
* @fileoverview JSON performance tests.
17
*/
18
19
goog.provide('goog.jsonPerf');
20
21
goog.require('goog.dom');
22
goog.require('goog.json');
23
goog.require('goog.math');
24
goog.require('goog.string');
25
goog.require('goog.testing.PerformanceTable');
26
goog.require('goog.testing.PropertyReplacer');
27
goog.require('goog.testing.jsunit');
28
29
goog.setTestOnly('goog.jsonPerf');
30
31
var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable'));
32
33
var stubs = new goog.testing.PropertyReplacer();
34
35
function tearDown() {
36
stubs.reset();
37
}
38
39
function testSerialize() {
40
var obj = populateObject({}, 50, 4);
41
42
table.run(function() {
43
var s = JSON.stringify(obj);
44
}, 'Stringify using JSON.stringify');
45
46
table.run(function() {
47
var s = goog.json.serialize(obj);
48
}, 'Stringify using goog.json.serialize');
49
}
50
51
function testParse() {
52
var obj = populateObject({}, 50, 4);
53
var s = JSON.stringify(obj);
54
55
table.run(function() { var o = JSON.parse(s); }, 'Parse using JSON.parse');
56
57
table.run(function() {
58
var o = goog.json.parse(s);
59
}, 'Parse using goog.json.parse');
60
61
table.run(function() {
62
var o = goog.json.unsafeParse(s);
63
}, 'Parse using goog.json.unsafeParse');
64
}
65
66
67
/**
68
* @param {!Object} obj The object to add properties to.
69
* @param {number} numProperties The number of properties to add.
70
* @param {number} depth The depth at which to recursively add properties.
71
* @return {!Object} The object given in obj (for convenience).
72
*/
73
function populateObject(obj, numProperties, depth) {
74
if (depth == 0) {
75
return randomLiteral();
76
}
77
78
// Make an object with a mix of strings, numbers, arrays, objects, booleans
79
// nulls as children.
80
for (var i = 0; i < numProperties; i++) {
81
var bucket = goog.math.randomInt(3);
82
switch (bucket) {
83
case 0:
84
obj[i] = randomLiteral();
85
break;
86
case 1:
87
obj[i] = populateObject({}, numProperties, depth - 1);
88
break;
89
case 2:
90
obj[i] = populateObject([], numProperties, depth - 1);
91
break;
92
}
93
}
94
return obj;
95
}
96
97
98
function randomLiteral() {
99
var bucket = goog.math.randomInt(3);
100
switch (bucket) {
101
case 0:
102
return goog.string.getRandomString();
103
case 1:
104
return Math.random();
105
case 2:
106
return Math.random() >= .5;
107
}
108
return null;
109
}
110
111