<script src="https://unpkg.com/lunr/lunr.js"></script>
<script type="text/javascript" src="https://rawgit.com/bvaughn/js-search/1.2.0/dist/js-search.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/minisearch@2.2.2/dist/umd/index.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
var books = [];
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var json = JSON.parse(xmlhttp.responseText);
books = json.books;
}
}
xmlhttp.open('GET', 'http://bvaughn.github.io/js-search/books.json', true);
xmlhttp.send();
};
</script>
var idx = lunr(function () {
this.ref('isbn');
this.field('title');
this.field('author');
books.forEach(function (doc) {
this.add(doc)
}, this);
})
var search = new JsSearch.Search('isbn');
search.searchIndex = new JsSearch.TfIdfSearchIndex('isbn');
search.addIndex('title');
search.addIndex('author');
search.addDocuments(books);
var miniSearch = new MiniSearch({
fields: ['isbn', 'title', 'author'], // fields to index for full-text search
});
miniSearch.addAll(books)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lunr | |
js search | |
minisearch |
Test name | Executions per second |
---|---|
Lunr | 2600294.2 Ops/sec |
js search | 22075126.0 Ops/sec |
minisearch | 1373556.4 Ops/sec |
Measuring the performance of JavaScript search libraries can be complex, but I'll break it down for you.
The provided JSON represents three benchmark test cases:
lunr
function is defined with three fields: ISBN, title, and author.MiniSearch
constructor is configured with three fields: ISBN, title, and author.Let's discuss each approach:
Lunr
Pros:
Cons:
js search
Pros:
Cons:
Minisearch
Pros:
MiniSearch
constructor provides an easy-to-use interface for creating indexes and adding documents.Cons:
Other alternatives to consider:
When testing these libraries, it's essential to consider factors like performance, ease of use, and customizability to determine which one best suits your project's needs.