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