我应该如何存储和检索字典单词

时间:2021-07-04 07:11:17

I have a JavaScript variable that holds an array of dictionary words like

我有一个JavaScript变量,它包含一系列字典单词

var words = ['and','cat', n1, n2, n3 and so on ]

This array holds about 58020 words.

这个数组大约有58020个单词。

What i have done is created an auto complete jQuery plugin that displays the words from the dictionary array in a drop down list when the user starts typing text into the text box. But the browser crashes at some point because I think the looping through each word is making the process slow.

我所做的是创建一个自动完成的jQuery插件,当用户开始在文本框中键入文本时,该插件在下拉列表中显示字典数组中的单词。但浏览器在某些时候崩溃了,因为我认为循环每个单词会使进程变慢。

How can i overcome this?

我怎么能克服这个?

Here is the function that checks the word array and outputs the words if found

这是检查单词数组并输出单词的函数(如果找到)

$(textInput).keyup(function(e) {

            var text = $(this).val();
            var foundTag = false;

            for (var i = 0; i < settings.tags.length; i++) {

                var tagName = settings.tags[i].toLowerCase();
                if (tagName.startsWith(text)) {

                    if (text != '') {
                        foundTag = true;
                        $(settings.tagContainer).append(GetDivDropDownItem(settings.tags[i]));
                    }
                    else {
                    }

                }

            }

        });

Edit

$(textInput).keyup(function(e) {

            var text = $(this).val();
            var foundTag = false;

            for (var i = 0; i < settings.words.length; i++) {

                var tagName = settings.words[i].toLowerCase();
                if (tagName.startsWith(text)) {

                    if (text != '') {
                        foundTag = true;
                        $(settings.tagContainer).append(GetDivDropDownItem(settings.words[i]));
                    }
                    else {
                    }

                }

            }

        });


var GetDivDropDownItem = function(text) {

        var cWidth = $(container).css("width");
        cWidth = cWidth.split("px")[0];

        var tag = $("<div/>");
        $(tag).css("paddingLeft", "5px");
        $(tag).css("paddingRight", "5px");
        $(tag).css("paddingBottom", "5px");
        $(tag).css("paddingTop", "5px");
        $(tag).css("width", cWidth - 10);
        $(tag).css("float", "left");

        $(tag).css("fontFamily", "Arial");
        $(tag).css("fontSize", "12px");
        $(tag).css("color", "#6A6B6C");
        $(tag).text(text);

        return $(tag);

    };

5 个解决方案

#1


2  

By not putting 58.000 words in a Javascript array.

通过不在Javascript数组中放入58.000个单词。

Use a webservice that holds all the dictionary words in a database, and query that.

使用包含数据库中所有字典单词的Web服务,并查询它。


edit: If you really insist on storing this in a javascript array, group the words by their first two characters. Easy to implement and around 600 times faster already.

编辑:如果您真的坚持将其存储在javascript数组中,请按前两个字符对单词进行分组。易于实施,速度提高约600倍。

#2


3  

You need to use better datastructures and algorithms. In general, I would suggest doing some research on pre-existing work before trying to tackle any problem.

您需要使用更好的数据结构和算法。总的来说,我建议在尝试解决任何问题之前先对已有的工作进行一些研究。

This is an article that may be of help: http://orion.lcg.ufrj.br/Dr.Dobbs/books/book5/chap08.htm

这篇文章可能会有所帮助:http://orion.lcg.ufrj.br/Dr.Dobbs/books/book5/chap08.htm

#3


3  

See this benchmarks and comparisons done by jQuery creator John Resig:

查看jQuery创建者John Resig所做的基准测试和比较:

http://ejohn.org/blog/revised-javascript-dictionary-search/

http://ejohn.org/blog/revised-javascript-dictionary-search/

Basically the answer is a simple trie structure, if you really want to do it pure-JS.

基本上答案是一个简单的trie结构,如果你真的想做纯JS。

#4


2  

A trie data structure would be good for a dictionnary.

特里数据结构对于词典是有益的。

#5


1  

Assuming settings.tags is the array of dictionary words, this code is going to be very cumbersome, since you're looping through the entire array with each keyup event.

假设settings.tags是字典单词的数组,这个代码将非常麻烦,因为你在每个keyup事件中循环遍历整个数组。

I would suggest that you organize the dictionary words in a structure that allows you to go to the words very quickly. Perhaps a binary tree or just an associative array.

我建议你在一个结构中组织字典单词,这样你就可以很快地找到单词。也许是二叉树或只是一个关联数组。

#1


2  

By not putting 58.000 words in a Javascript array.

通过不在Javascript数组中放入58.000个单词。

Use a webservice that holds all the dictionary words in a database, and query that.

使用包含数据库中所有字典单词的Web服务,并查询它。


edit: If you really insist on storing this in a javascript array, group the words by their first two characters. Easy to implement and around 600 times faster already.

编辑:如果您真的坚持将其存储在javascript数组中,请按前两个字符对单词进行分组。易于实施,速度提高约600倍。

#2


3  

You need to use better datastructures and algorithms. In general, I would suggest doing some research on pre-existing work before trying to tackle any problem.

您需要使用更好的数据结构和算法。总的来说,我建议在尝试解决任何问题之前先对已有的工作进行一些研究。

This is an article that may be of help: http://orion.lcg.ufrj.br/Dr.Dobbs/books/book5/chap08.htm

这篇文章可能会有所帮助:http://orion.lcg.ufrj.br/Dr.Dobbs/books/book5/chap08.htm

#3


3  

See this benchmarks and comparisons done by jQuery creator John Resig:

查看jQuery创建者John Resig所做的基准测试和比较:

http://ejohn.org/blog/revised-javascript-dictionary-search/

http://ejohn.org/blog/revised-javascript-dictionary-search/

Basically the answer is a simple trie structure, if you really want to do it pure-JS.

基本上答案是一个简单的trie结构,如果你真的想做纯JS。

#4


2  

A trie data structure would be good for a dictionnary.

特里数据结构对于词典是有益的。

#5


1  

Assuming settings.tags is the array of dictionary words, this code is going to be very cumbersome, since you're looping through the entire array with each keyup event.

假设settings.tags是字典单词的数组,这个代码将非常麻烦,因为你在每个keyup事件中循环遍历整个数组。

I would suggest that you organize the dictionary words in a structure that allows you to go to the words very quickly. Perhaps a binary tree or just an associative array.

我建议你在一个结构中组织字典单词,这样你就可以很快地找到单词。也许是二叉树或只是一个关联数组。