https://github.com/noomorph/js-lcs
Longest common substring implementation in JavaScript
https://github.com/noomorph/js-lcs
Last synced: 8 months ago
JSON representation
Longest common substring implementation in JavaScript
- Host: GitHub
- URL: https://github.com/noomorph/js-lcs
- Owner: noomorph
- License: mit
- Created: 2018-11-02T15:45:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-02T09:50:54.000Z (over 7 years ago)
- Last Synced: 2025-09-29T12:14:16.971Z (9 months ago)
- Language: TypeScript
- Size: 60.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# js-lcs
[](https://badge.fury.io/js/js-lcs)
[](https://opensource.org/licenses/MIT)
[](https://travis-ci.org/noomorph/js-lcs)
Partial[1](#footnote1) implementation of [Longest common substring problem](https://en.wikipedia.org/wiki/Longest_common_substring_problem) in TypeScript/JavaScript, relatively fast and memory-optimized[2](#footnote2).
## Usage
### Simple
```js
import { LCS } from 'js-lcs';
LCS.size("aababcabcdabcaba", "eabcde"); // 4
```
### Advanced
You can get more performance if you switch to typed arrays like *Uint8Array, Uint16Array*, and instantiate only one instance of LCS class:
```js
import { LCS } from 'js-lcs';
const rawFiles = [
// Uint16Array of char codes in file_1.txt
// ...
// Uint16Array of char codes in file_n.txt
];
const maxSize = Math.max(...rawFiles.map(f => f.length));
const lcs = new LCS({ maxSize }); // in this way you reuse once allocated memory for every lcs.size() call
for (let i = 0; i < rawFiles.length; i++) {
for (let j = 0; j < i; j++) {
const [a, b] = [rawFiles[j], rawFiles[i]];
console.log(lcs.size(a, b));
}
}
}
```
Please always make sure you don't mix types of arguments to `lcs.size()`,
e.g. if you start passing typed arrays, do not pass strings anymore and vice versa.
Otherwise, you risk getting `size()` function deoptimized by V8.
#### Footnotes
1. At the moment, it can calculate only size of the longest common substring, not the string itself.
2. Processing two 3,000-char strings takes under 2 seconds, however, memory consumption still can be improved a bit, from `O(max(r, n))` to `O(min(r, n))` with a little pull request. Performance improvements are very welcome!