hang.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function main() {
  6. //var s0 = "aaa";
  7. var s0 = "aaaaaaaaaaaaaaa";
  8. var s1 = s0 + "aaaaaa";
  9. var s2 = s1 + "aa";
  10. var s3 = s2 + "aaaa";
  11. var s4 = s3 + "aaaa";
  12. var s5 = s4;
  13. for(var i = 0; i < 6; ++i)
  14. s5 += s5;
  15. var end = "caab";
  16. s0 += end;
  17. s1 += end;
  18. s2 += end;
  19. s3 += end;
  20. s4 += end;
  21. s5 += end;
  22. measureTime(function () {
  23. match(/(?:a+)+b/, s2);
  24. });
  25. measureTime(function () {
  26. match(/(?:a+)+ab/, s2);
  27. });
  28. echo("");
  29. measureTime(function () {
  30. match(/(?:a|aa)+b/, s4);
  31. });
  32. measureTime(function () {
  33. match(/(?:a|aa)+ab/, s4);
  34. });
  35. echo("");
  36. measureTime(function () {
  37. match(/(?:a|a?)+b/, s1);
  38. });
  39. measureTime(function () {
  40. match(/(?:a|a?)+ab/, s1);
  41. });
  42. echo("");
  43. measureTime(function () {
  44. match(/(?:(?:a{1,10})+)+b/, s0);
  45. });
  46. measureTime(function () {
  47. match(/(?:a{1,10})+ab/, s2);
  48. });
  49. echo("");
  50. measureTime(function () {
  51. match(/(?:a+){10}b/, s3);
  52. });
  53. measureTime(function () {
  54. match(/(?:a+){10}ab/, s3);
  55. });
  56. echo("");
  57. measureTime(function () {
  58. match(/(?:a|a?){12}b/, s2);
  59. });
  60. measureTime(function () {
  61. match(/(?:a|a?){12}ab/, s2);
  62. });
  63. echo("");
  64. measureTime(function () {
  65. match(/a*?a*b/, s5);
  66. });
  67. measureTime(function () {
  68. match(/a*?a*b/, s5);
  69. });
  70. echo("");
  71. measureTime(function () {
  72. match(/(?:(a+)(?:\1+))+ab/, s3);
  73. });
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. if(this.WScript || !this.document && this.print)
  77. main();
  78. function measureTime(f) {
  79. var d = new Date().getTime();
  80. f();
  81. echo(new Date().getTime() - d);
  82. }
  83. function myToString(o, quoteStrings) {
  84. switch(o) {
  85. case null:
  86. case undefined:
  87. case -Infinity:
  88. case Infinity:
  89. return "" + o;
  90. }
  91. switch(typeof o) {
  92. case "boolean":
  93. return "" + o;
  94. case "number":
  95. {
  96. if(o === 0 && 1 / o < 0)
  97. return "-0";
  98. var s = "" + o;
  99. var i = s.indexOf("e");
  100. var end = i === -1 ? s.length : e;
  101. i = s.indexOf(".");
  102. var start = i === -1 ? 0 : i + 1;
  103. if(start !== 0) {
  104. if((end - start) % 3 !== 0)
  105. end += 3 - (end - start) % 3;
  106. for(i = end - 3; i > start; i -= 3)
  107. s = s.substring(0, i) + "," + s.substring(i);
  108. end = start - 1;
  109. start = 0;
  110. }
  111. for(i = end - 3; i > start; i -= 3)
  112. s = s.substring(0, i) + "," + s.substring(i);
  113. return s;
  114. }
  115. case "string":
  116. {
  117. var hex = "0123456789abcdef";
  118. var s = "";
  119. for(var i = 0; i < o.length; ++i) {
  120. var c = o.charCodeAt(i);
  121. switch(c) {
  122. case 0x0:
  123. s += "\\0";
  124. continue;
  125. case 0x8:
  126. s += "\\b";
  127. continue;
  128. case 0xb:
  129. s += "\\v";
  130. continue;
  131. case 0xc:
  132. s += "\\f";
  133. continue;
  134. }
  135. if(quoteStrings) {
  136. switch(c) {
  137. case 0x9:
  138. s += "\\t";
  139. continue;
  140. case 0xa:
  141. s += "\\n";
  142. continue;
  143. case 0xd:
  144. s += "\\r";
  145. continue;
  146. case 0x22:
  147. s += "\\\"";
  148. continue;
  149. case 0x5c:
  150. s += "\\\\";
  151. continue;
  152. }
  153. }
  154. if(c >= 0x20 && c < 0x7f)
  155. s += o.charAt(i);
  156. else if(c <= 0xff)
  157. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  158. else
  159. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  160. }
  161. if(quoteStrings)
  162. s = "\"" + s + "\"";
  163. return s;
  164. }
  165. case "object":
  166. case "function":
  167. break;
  168. default:
  169. return "<unknown type '" + typeof o + "'>";
  170. }
  171. if(o instanceof Array) {
  172. var s = "[";
  173. for(var i = 0; i < o.length; ++i) {
  174. if(i)
  175. s += ", ";
  176. s += myToString(o[i], true);
  177. }
  178. return s + "]";
  179. }
  180. if(o instanceof Error)
  181. return o.name + ": " + o.message;
  182. if(o instanceof RegExp)
  183. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  184. if(o instanceof Object && !(o instanceof Function)) {
  185. var s = "";
  186. for(var p in o)
  187. s += myToString(p) + ": " + myToString(o[p], true) + ", ";
  188. if(s.length !== 0)
  189. s = s.substring(0, s.length - ", ".length);
  190. return "{" + s + "}";
  191. }
  192. return "" + o;
  193. }
  194. function echo() {
  195. var doEcho;
  196. if(this.WScript)
  197. doEcho = function (s) { this.WScript.Echo(s); };
  198. else if(this.document)
  199. doEcho = function (s) {
  200. var div = this.document.createElement("div");
  201. div.innerText = s;
  202. this.document.body.appendChild(div);
  203. };
  204. else
  205. doEcho = function (s) { this.print(s); };
  206. echo = function () {
  207. var s = "";
  208. for(var i = 0; i < arguments.length; ++i)
  209. s += myToString(arguments[i]);
  210. doEcho(s);
  211. };
  212. echo.apply(this, arguments);
  213. }
  214. function safeCall(f) {
  215. var args = [];
  216. for(var a = 1; a < arguments.length; ++a)
  217. args.push(arguments[a]);
  218. try {
  219. return f.apply(this, args);
  220. } catch(ex) {
  221. echo(ex);
  222. }
  223. }
  224. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. function exec(r, s) {
  226. echo("exec(", r, ", ", myToString(s, true), ");");
  227. var result = r.exec(s);
  228. echo(result);
  229. return result;
  230. }
  231. function test(r, s) {
  232. echo("test(", r, ", ", myToString(s, true), ");");
  233. var result = r.test(s);
  234. echo(result);
  235. return result;
  236. }
  237. function match(r, s) {
  238. echo("match(", r, ", ", myToString(s, true), ");");
  239. var result = s.match(r);
  240. echo(result);
  241. return result;
  242. }
  243. function replace(r, s, w) {
  244. echo("replace(", myToString(r, true), ", ", myToString(s, true), ", ", myToString(w, true), ");");
  245. var result = s.replace(r, w);
  246. echo(result);
  247. return result;
  248. }
  249. function split(r, s) {
  250. echo("split(", r, ", ", myToString(s, true), ");");
  251. var result = s.split(r);
  252. echo(result);
  253. return result;
  254. }
  255. function search(r, s) {
  256. echo("search(", r, ", ", myToString(s, true), ");");
  257. var result = s.search(r);
  258. echo(result);
  259. return result;
  260. }