bench.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. exports.__esModule = true;
  3. /* eslint-disable @typescript-eslint/no-var-requires */
  4. /* eslint-disable no-console */
  5. var Benchmark = require("benchmark");
  6. var mod_js_1 = require("./mod.js");
  7. var fast_levenshtein_1 = require("fast-levenshtein");
  8. var fs = require("fs");
  9. var jslevenshtein = require("js-levenshtein");
  10. var leven = require("leven");
  11. var levenshteinEditDistance = require("levenshtein-edit-distance");
  12. var suite = new Benchmark.Suite();
  13. var randomstring = function (length) {
  14. var result = "";
  15. var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  16. var charactersLength = characters.length;
  17. for (var i = 0; i < length; i++) {
  18. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  19. }
  20. return result;
  21. };
  22. var randomstringArr = function (stringSize, arraySize) {
  23. var i = 0;
  24. var arr = [];
  25. for (i = 0; i < arraySize; i++) {
  26. arr.push(randomstring(stringSize));
  27. }
  28. return arr;
  29. };
  30. var arrSize = 1000;
  31. if (!fs.existsSync("data.json")) {
  32. var data_1 = [
  33. randomstringArr(4, arrSize),
  34. randomstringArr(8, arrSize),
  35. randomstringArr(16, arrSize),
  36. randomstringArr(32, arrSize),
  37. randomstringArr(64, arrSize),
  38. randomstringArr(128, arrSize),
  39. randomstringArr(256, arrSize),
  40. randomstringArr(512, arrSize),
  41. randomstringArr(1024, arrSize),
  42. ];
  43. fs.writeFileSync("data.json", JSON.stringify(data_1));
  44. }
  45. var data = JSON.parse(fs.readFileSync("data.json", "utf8"));
  46. var _loop_1 = function (i) {
  47. var datapick = data[i];
  48. if (process.argv[2] !== "no") {
  49. suite
  50. .add("".concat(i, " - js-levenshtein"), function () {
  51. for (var j = 0; j < arrSize - 1; j += 2) {
  52. jslevenshtein(datapick[j], datapick[j + 1]);
  53. }
  54. })
  55. .add("".concat(i, " - leven"), function () {
  56. for (var j = 0; j < arrSize - 1; j += 2) {
  57. leven(datapick[j], datapick[j + 1]);
  58. }
  59. })
  60. .add("".concat(i, " - fast-levenshtein"), function () {
  61. for (var j = 0; j < arrSize - 1; j += 2) {
  62. (0, fast_levenshtein_1.get)(datapick[j], datapick[j + 1]);
  63. }
  64. })
  65. .add("".concat(i, " - levenshtein-edit-distance"), function () {
  66. for (var j = 0; j < arrSize - 1; j += 2) {
  67. levenshteinEditDistance(datapick[j], datapick[j + 1]);
  68. }
  69. });
  70. }
  71. suite.add("".concat(i, " - fastest-levenshtein"), function () {
  72. for (var j = 0; j < arrSize - 1; j += 2) {
  73. (0, mod_js_1.distance)(datapick[j], datapick[j + 1]);
  74. }
  75. });
  76. };
  77. // BENCHMARKS
  78. for (var i = 0; i < 9; i++) {
  79. _loop_1(i);
  80. }
  81. var results = new Map();
  82. suite
  83. .on("cycle", function (event) {
  84. console.log(String(event.target));
  85. if (results.has(event.target.name[0])) {
  86. results.get(event.target.name[0]).push(event.target.hz);
  87. }
  88. else {
  89. results.set(event.target.name[0], [event.target.hz]);
  90. }
  91. })
  92. .on("complete", function () {
  93. console.log(results);
  94. })
  95. // run async
  96. .run({ async: true });