json_perf.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * @fileoverview JSON performance tests.
  16. */
  17. goog.provide('goog.jsonPerf');
  18. goog.require('goog.dom');
  19. goog.require('goog.json');
  20. goog.require('goog.math');
  21. goog.require('goog.string');
  22. goog.require('goog.testing.PerformanceTable');
  23. goog.require('goog.testing.PropertyReplacer');
  24. goog.require('goog.testing.jsunit');
  25. goog.setTestOnly('goog.jsonPerf');
  26. var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable'));
  27. var stubs = new goog.testing.PropertyReplacer();
  28. function tearDown() {
  29. stubs.reset();
  30. }
  31. function testSerialize() {
  32. var obj = populateObject({}, 50, 4);
  33. table.run(function() {
  34. var s = JSON.stringify(obj);
  35. }, 'Stringify using JSON.stringify');
  36. table.run(function() {
  37. var s = goog.json.serialize(obj);
  38. }, 'Stringify using goog.json.serialize');
  39. }
  40. function testParse() {
  41. var obj = populateObject({}, 50, 4);
  42. var s = JSON.stringify(obj);
  43. table.run(function() { var o = JSON.parse(s); }, 'Parse using JSON.parse');
  44. table.run(function() {
  45. var o = goog.json.parse(s);
  46. }, 'Parse using goog.json.parse');
  47. table.run(function() {
  48. var o = goog.json.unsafeParse(s);
  49. }, 'Parse using goog.json.unsafeParse');
  50. }
  51. /**
  52. * @param {!Object} obj The object to add properties to.
  53. * @param {number} numProperties The number of properties to add.
  54. * @param {number} depth The depth at which to recursively add properties.
  55. * @return {!Object} The object given in obj (for convenience).
  56. */
  57. function populateObject(obj, numProperties, depth) {
  58. if (depth == 0) {
  59. return randomLiteral();
  60. }
  61. // Make an object with a mix of strings, numbers, arrays, objects, booleans
  62. // nulls as children.
  63. for (var i = 0; i < numProperties; i++) {
  64. var bucket = goog.math.randomInt(3);
  65. switch (bucket) {
  66. case 0:
  67. obj[i] = randomLiteral();
  68. break;
  69. case 1:
  70. obj[i] = populateObject({}, numProperties, depth - 1);
  71. break;
  72. case 2:
  73. obj[i] = populateObject([], numProperties, depth - 1);
  74. break;
  75. }
  76. }
  77. return obj;
  78. }
  79. function randomLiteral() {
  80. var bucket = goog.math.randomInt(3);
  81. switch (bucket) {
  82. case 0:
  83. return goog.string.getRandomString();
  84. case 1:
  85. return Math.random();
  86. case 2:
  87. return Math.random() >= .5;
  88. }
  89. return null;
  90. }