fastest-levenshtein
Advanced tools
Comparing version 1.0.11 to 1.0.12
{ | ||
"name": "fastest-levenshtein", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Fastest Levenshtein distance implementation in JS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
135
test.js
@@ -1,27 +0,43 @@ | ||
"use strict"; | ||
const {distance, closest} = require("./index.js"); | ||
const { distance, closest } = require("./index.js"); | ||
const levenshtein = require("js-levenshtein"); | ||
const levenshtein = (a, b) => { | ||
if (a.length === 0) return b.length; | ||
if (b.length === 0) return a.length; | ||
const rnd_string = (length) => { | ||
let result = ""; | ||
for (let i = 0; i < length; i++) { | ||
result += String.fromCharCode(Math.floor(Math.random() * 0x10000)); | ||
if (a.length > b.length) { | ||
const tmp = a; | ||
a = b; | ||
b = tmp; | ||
} | ||
return result; | ||
} | ||
const rnd_string_ascii = (length) => { | ||
let result = ""; | ||
for (let i = 0; i < length; i++) { | ||
result += String.fromCharCode(Math.floor(Math.random() * 256)); | ||
const row = []; | ||
for (let i = 0; i <= a.length; i++) { | ||
row[i] = i; | ||
} | ||
return result; | ||
} | ||
const rnd_string_alpha = (length) => { | ||
for (let i = 1; i <= b.length; i++) { | ||
let prev = i; | ||
for (let j = 1; j <= a.length; j++) { | ||
let val; | ||
if (b.charAt(i - 1) === a.charAt(j - 1)) { | ||
val = row[j - 1]; | ||
} else { | ||
val = Math.min(row[j - 1] + 1, prev + 1, row[j] + 1); | ||
} | ||
row[j - 1] = prev; | ||
prev = val; | ||
} | ||
row[a.length] = prev; | ||
} | ||
return row[a.length]; | ||
}; | ||
function makeid(length) { | ||
let result = ""; | ||
const chars = "ABCD" | ||
const characters = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
const charactersLength = characters.length; | ||
for (let i = 0; i < length; i++) { | ||
result += chars[((Math.random() * chars.length) | 0) % chars.length]; | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
@@ -31,70 +47,19 @@ return result; | ||
// Benchmark | ||
// let b = 0; | ||
// console.time('test'); | ||
// for (let i = 0; i < 1000000; i++) { | ||
// const rnd_num1 = 32 | ||
// const rnd_num2 = 32 | ||
// const rnd_string1 = makeid(rnd_num1); | ||
// const rnd_string2 = makeid(rnd_num2); | ||
// const actual = distance(rnd_string1, rnd_string2); | ||
// b += actual; | ||
// } | ||
// console.timeEnd('test'); | ||
// console.log(b) | ||
// Test | ||
for (let i = 0; i < Infinity; i++) { | ||
if (i % 10000 === 0) console.log(i) | ||
const max_len = 100; | ||
const rnd_num1 = (Math.random() * max_len) | 0; | ||
const rnd_num2 = (Math.random() * max_len) | 0; | ||
const rnd_num3 = (Math.random() * max_len) | 0; | ||
const rnd_num4 = (Math.random() * max_len) | 0; | ||
const rnd_num5 = (Math.random() * max_len) | 0; | ||
const rnd_num6 = (Math.random() * max_len) | 0; | ||
const rnd_string1 = rnd_string(rnd_num1); | ||
const rnd_string2 = rnd_string(rnd_num2); | ||
const rnd_string3 = rnd_string(rnd_num3); | ||
const rnd_string4 = rnd_string(rnd_num4); | ||
const rnd_string5 = rnd_string(rnd_num5); | ||
const rnd_string6 = rnd_string(rnd_num6); | ||
const actual1 = distance(rnd_string1, rnd_string2); | ||
const expected1 = levenshtein(rnd_string1, rnd_string2); | ||
const actual2 = distance(rnd_string3, rnd_string4); | ||
const expected2 = levenshtein(rnd_string3, rnd_string4); | ||
const actual3 = distance(rnd_string5, rnd_string6); | ||
const expected3 = levenshtein(rnd_string5, rnd_string6); | ||
if (actual1 !== expected1) { | ||
console.log(rnd_string1) | ||
console.log(rnd_string2) | ||
test("test compare", () => { | ||
const errors = 0; | ||
for (let i = 0; i < 1000; i++) { | ||
const rnd_num1 = (Math.random() * 1000) | 0; | ||
const rnd_num2 = (Math.random() * 1000) | 0; | ||
const rnd_string1 = makeid(rnd_num1); | ||
const rnd_string2 = makeid(rnd_num2); | ||
const actual = distance(rnd_string1, rnd_string2); | ||
const expected = levenshtein(rnd_string1, rnd_string2); | ||
expect(actual).toBe(expected); | ||
} | ||
if (actual2 !== expected2) { | ||
console.log(rnd_string1) | ||
console.log(rnd_string2) | ||
} | ||
if (actual3 !== expected3) { | ||
console.log(rnd_string1) | ||
console.log(rnd_string2) | ||
} | ||
} | ||
}); | ||
// test("test compare", () => { | ||
// for (let i = 0; i < 1000; i++) { | ||
// const rnd_num1 = (Math.random() * 1000) | 0; | ||
// const rnd_num2 = (Math.random() * 1000) | 0; | ||
// const rnd_string1 = makeid(rnd_num1); | ||
// const rnd_string2 = makeid(rnd_num2); | ||
// const actual = distance(rnd_string1, rnd_string2); | ||
// const expected = levenshtein(rnd_string1, rnd_string2); | ||
// expect(actual).toBe(expected); | ||
// } | ||
// }); | ||
// test("test find", () => { | ||
// const actual = closest("fast", ["slow", "faster", "fastest"]); | ||
// const expected = "faster"; | ||
// expect(actual).toBe(expected); | ||
// }); | ||
test("test find", () => { | ||
const actual = closest("fast", ["slow", "faster", "fastest"]); | ||
const expected = "faster"; | ||
expect(actual).toBe(expected); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11002
9
227