Skip to content

search: very slow and inefficient index initialization #739

Closed
@evil-shrike

Description

@evil-shrike

Hi.
I have an index (search.js) with 35K rows (9MB). It takes more than 35 seconds to initialize. So user have to click in search field and wait for half a minute. Obviously it's ridiculous.
I looked up into the code where index is being initialized. It seems it's very inefficient.

        function createIndex() {
            index = new lunr.Index();
            index.pipeline.add(lunr.trimmer);
            index.field("name", {
                boost: 10
            });
            index.field("parent");
            index.ref("id");
            var rows = search.data.rows;
            var pos = 0;
            var length = rows.length;
            function batch() {
                var cycles = 0;
                while (cycles++ < 100) {
                    index.add(rows[pos]);
                    if (++pos == length) {
                        return setLoadingState(SearchLoadingState.Ready);
                    }
                }
                setTimeout(batch, 10);
            }
            batch();
        }

I understand that you're trying not to block ui thread splitting work on tasks being executed via setTimeout. But the end result is not good.
But besides that why do you add every rows via Index.add?
Lunr Index has load method - https://lunrjs.com/docs/lunr.Index.html which should be MUCH faster. All we need is just to build a proper serialized index during building stage instead of that custom structure with rows.

I have search on one of my static doc site with lurn, and 8MB lunr-index initialized pretty fast.

		$.getJSON("search_index.json", function (data) {
   			if (lunr.multiLanguage) {
				lunr.multiLanguage('en', 'ru');
			}
			engine = lunr.Index.load(data.lunrIndex);
			doSearch(engine, term, data.pageIndex, container);
		});

(pageIndex is a custom structure for mapping urls to titles)

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions are especially encouraged

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions