set_perf.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2009 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <!--
  9. -->
  10. <head>
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <title>Closure Performance Tests - goog.ui.Set vs goog.ui.StringSet</title>
  13. <link rel="stylesheet" type="text/css" href="../testing/performancetable.css"/>
  14. <script src="../base.js"></script>
  15. <script>
  16. goog.require('goog.functions');
  17. goog.require('goog.string');
  18. goog.require('goog.structs.Set');
  19. goog.require('goog.structs.StringSet');
  20. goog.require('goog.testing.PerformanceTable');
  21. goog.require('goog.testing.PropertyReplacer');
  22. goog.require('goog.testing.jsunit');
  23. </script>
  24. </head>
  25. <body>
  26. <h1>goog.ui.Set and goog.ui.StringSet Performance Tests</h1>
  27. <p>
  28. <strong>User-agent:</strong>
  29. <script>document.write(navigator.userAgent);</script>
  30. </p>
  31. <div id="perfTable"></div>
  32. <hr>
  33. <script>
  34. var table = new goog.testing.PerformanceTable(
  35. goog.dom.getElement('perfTable'));
  36. // Number of operations to measure in each table line.
  37. var OPS_COUNT = 100000;
  38. var stubs = new goog.testing.PropertyReplacer();
  39. function tearDown() {
  40. stubs.reset();
  41. }
  42. function testCreateSetFromArrayWithoutRepetition() {
  43. var values = []
  44. for (var i = 0; i < OPS_COUNT; i++) {
  45. values.push(i);
  46. }
  47. table.run(function() {
  48. var s = new goog.structs.StringSet(values);
  49. }, 'Create string set from number array without repetition');
  50. values = []
  51. for (var i = 0; i < OPS_COUNT; i++) {
  52. values.push(String(i));
  53. }
  54. table.run(function() {
  55. var s = new goog.structs.StringSet(values);
  56. }, 'Create string set from string array without repetition');
  57. }
  58. function testCreateSetWithoutRepetition() {
  59. table.run(function() {
  60. var s = new goog.structs.Set();
  61. for (var i = 0; i < OPS_COUNT; i++) {
  62. s.add(i);
  63. }
  64. }, 'Add elements to set without repetition');
  65. table.run(function() {
  66. var s = new goog.structs.StringSet();
  67. for (var i = 0; i < OPS_COUNT; i++) {
  68. s.add(i);
  69. }
  70. }, 'Add elements to string set without repetition');
  71. stubs.replace(goog.structs.StringSet, 'encode_', goog.functions.identity);
  72. stubs.replace(goog.structs.StringSet, 'decode_', goog.functions.identity);
  73. table.run(function() {
  74. var s = new goog.structs.StringSet();
  75. for (var i = 0; i < OPS_COUNT; i++) {
  76. s.add(i);
  77. }
  78. }, 'Add elements to string set without repetition and escaping');
  79. }
  80. function testCreateSetWithRepetition() {
  81. table.run(function() {
  82. var s = new goog.structs.Set();
  83. for (var n = 0; n < 10 ; n++) {
  84. for (var i = 0; i < OPS_COUNT / 10; i++) {
  85. s.add(i);
  86. }
  87. }
  88. }, 'Add elements to set with repetition');
  89. table.run(function() {
  90. var s = new goog.structs.StringSet();
  91. for (var n = 0; n < 10; n++) {
  92. for (var i = 0; i < OPS_COUNT / 10; i++) {
  93. s.add(i);
  94. }
  95. }
  96. }, 'Add elements to string set with repetition');
  97. }
  98. function testGetCount() {
  99. var bigSet = new goog.structs.Set;
  100. var bigStringSet = new goog.structs.StringSet;
  101. for (var i = 0; i < OPS_COUNT; i++) {
  102. bigSet.add(i);
  103. bigStringSet.add(i);
  104. }
  105. table.run(function() {
  106. bigSet.getCount();
  107. }, 'Count the number of elements in a set');
  108. table.run(function() {
  109. bigStringSet.getCount();
  110. }, 'Count the number of elements in a string set');
  111. }
  112. function testGetValues() {
  113. var bigSet = new goog.structs.Set;
  114. var bigStringSet = new goog.structs.StringSet;
  115. for (var i = 0; i < OPS_COUNT; i++) {
  116. bigSet.add(i);
  117. bigStringSet.add(i);
  118. }
  119. table.run(function() {
  120. bigSet.getValues();
  121. }, 'Convert a set to array');
  122. table.run(function() {
  123. bigStringSet.getValues();
  124. }, 'Convert a string set to array');
  125. }
  126. function testForEach() {
  127. var bigSet = new goog.structs.Set;
  128. var bigStringSet = new goog.structs.StringSet;
  129. for (var i = 0; i < OPS_COUNT; i++) {
  130. bigSet.add(i);
  131. bigStringSet.add(i);
  132. }
  133. table.run(function() {
  134. goog.structs.forEach(bigSet, goog.nullFunction);
  135. }, 'Iterate over set with forEach');
  136. table.run(function() {
  137. goog.structs.forEach(bigStringSet, goog.nullFunction);
  138. }, 'Iterate over string set with forEach');
  139. }
  140. function testForEachWithLargeKeys() {
  141. var bigSet = new goog.structs.Set;
  142. var bigStringSet = new goog.structs.StringSet;
  143. for (var i = 0; i < OPS_COUNT / 100; i++) {
  144. bigSet.add(goog.string.repeat(String(i), 1000));
  145. bigStringSet.add(goog.string.repeat(String(i), 1000));
  146. }
  147. table.run(function() {
  148. for (var i = 0; i < 100; i++) {
  149. goog.structs.forEach(bigSet, goog.nullFunction);
  150. }
  151. }, 'Iterate over set of large strings with forEach');
  152. table.run(function() {
  153. for (var i = 0; i < 100; i++) {
  154. goog.structs.forEach(bigStringSet, goog.nullFunction);
  155. }
  156. }, 'Iterate over string set of large strings with forEach');
  157. }
  158. function testAddRemove() {
  159. table.run(function() {
  160. var s = new goog.structs.Set();
  161. for (var i = 0; i < OPS_COUNT / 2; i++) {
  162. s.add(i);
  163. }
  164. for (var i = 0; i < OPS_COUNT / 2; i++) {
  165. s.remove(i);
  166. }
  167. }, 'Add then remove elements from set');
  168. table.run(function() {
  169. var s = new goog.structs.StringSet();
  170. for (var i = 0; i < OPS_COUNT / 2; i++) {
  171. s.add(i);
  172. }
  173. for (var i = 0; i < OPS_COUNT / 2; i++) {
  174. s.remove(i);
  175. }
  176. }, 'Add then remove elements from string set');
  177. }
  178. function testContains() {
  179. var bigSet = new goog.structs.Set;
  180. var bigStringSet = new goog.structs.StringSet;
  181. var arr = [];
  182. for (var i = 0; i < OPS_COUNT; i++) {
  183. bigSet.add(i);
  184. bigStringSet.add(i);
  185. arr.push(i);
  186. }
  187. table.run(function() {
  188. for (var i = 0; i < OPS_COUNT; i++) {
  189. bigSet.contains(i);
  190. }
  191. }, 'Membership check for each element of set');
  192. table.run(function() {
  193. for (var i = 0; i < OPS_COUNT; i++) {
  194. bigStringSet.contains(i);
  195. }
  196. }, 'Membership check for each element of string set with contains');
  197. table.run(function() {
  198. bigStringSet.containsArray(arr);
  199. }, 'Membership check for each element of string set with containsArray');
  200. stubs.replace(goog.structs.StringSet, 'encode_', goog.functions.identity);
  201. stubs.replace(goog.structs.StringSet, 'decode_', goog.functions.identity);
  202. table.run(function() {
  203. for (var i = 0; i < OPS_COUNT; i++) {
  204. bigStringSet.contains(i);
  205. }
  206. }, 'Membership check for each element of string set without escaping');
  207. }
  208. function testEquals() {
  209. table.run(function() {
  210. var s1 = new goog.structs.Set();
  211. var s2 = new goog.structs.Set();
  212. for (var i = 0; i < OPS_COUNT / 4; i++) {
  213. s1.add(i);
  214. s2.add(i);
  215. }
  216. s1.equals(s2);
  217. }, 'Create then compare two sets');
  218. table.run(function() {
  219. var s1 = new goog.structs.StringSet();
  220. var s2 = new goog.structs.StringSet();
  221. for (var i = 0; i < OPS_COUNT / 4; i++) {
  222. s1.add(i);
  223. s2.add(i);
  224. }
  225. s1.equals(s2);
  226. }, 'Create then compare two string sets');
  227. }
  228. </script>
  229. </body>
  230. </html>