有什么办法可以让这个 JavaScript 选项卡补全脚本更加高效吗?

发布于 2024-08-31 20:41:07 字数 676 浏览 9 评论 0原文

此代码将集成到 AJAX 聊天系统中,以启用选项卡自动完成用户名:

var usernames = new Array();
usernames[0] = "Saladin";
usernames[1] = "Jyllaby";
usernames[2] = "CadaverKindler";
usernames[3] = "qbsuperstar03";

var text = "Text and something else q";

// Start of the script to be imported
var searchTerm = text.slice(text.lastIndexOf(" ") + 1);
var i;
for(i = 0; i < usernames.length && usernames[i].substr(0,searchTerm.length) != searchTerm; i++);
// End of the script to be imported

document.write(usernames[i]);

需要注意的几点: 用户名数组和文本变量都将通过 AJAX 从聊天本身加载(这,不幸的是,我不知道),最终的输出也将由 AJAX 处理。

有没有更有效的方法来做到这一点?

另外,关于如何处理找到的 searchTerm 的多个实例有什么提示吗?

This code is to be integrated into an AJAX Chat system to enable a tab auto-completion of user names:

var usernames = new Array();
usernames[0] = "Saladin";
usernames[1] = "Jyllaby";
usernames[2] = "CadaverKindler";
usernames[3] = "qbsuperstar03";

var text = "Text and something else q";

// Start of the script to be imported
var searchTerm = text.slice(text.lastIndexOf(" ") + 1);
var i;
for(i = 0; i < usernames.length && usernames[i].substr(0,searchTerm.length) != searchTerm; i++);
// End of the script to be imported

document.write(usernames[i]);

A couple of notes to be made: The array of usernames and the text variable would both be loaded from the chat itself via AJAX (which, unfortunately, I don't know), and the final output will be handled by AJAX as well.

Is there a more efficient way to do this?

Also, any tips on how to handle multiple instances of the searchTerm being found?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚笙 2024-09-07 20:41:07

微观优化:而不是获取子字符串并比较它(创建大量临时字符串)......

usernames[i].substr(0,searchTerm.length) != searchTerm

您应该使用indexOf,它不会创建临时字符串...

usernames[i].indexOf(searchTerm) == 0

“searchTerm 的多个实例正在成立”?你能举一个你正在考虑的问题的例子吗?

Micro-optimization: instead of getting the substring and comparing it (creating lots of temporary strings)...

usernames[i].substr(0,searchTerm.length) != searchTerm

...you should use indexOf, which creates no temporary strings...

usernames[i].indexOf(searchTerm) == 0

What do you mean by "multiple instances of the searchTerm being found"? Can you give an example of the problem you're thinking of?

夏末染殇 2024-09-07 20:41:07

通过保持数组排序并使用二分搜索来查找匹配项,您可以显着提高效率(如果有大量用户)。

You can make this significantly more efficient (provided large number of users) by keeping the array sorted and using binary search to find the match.

蓦然回首 2024-09-07 20:41:07

现在编码的方式是:

for(i = 0; i < usernames.length && usernames[i].substr(0,searchTerm.length) != searchTerm; i++);

您正在检查用户名的长度和 searchTerm 的长度,并在每次循环时从 usernames[i] 中获取子字符串。

对于您不希望在循环过程中更改的任何这些,您应该在循环开始之前将它们存储在变量中。

从变量中获取值比检查对象属性或方法要快得多。

所以像这样:

for(i = 0,ii=usernames.length,j=searchTerm.length; i < ii && usernames[i].substr(0,j) != searchTerm; i++);

The way you have it coded now:

for(i = 0; i < usernames.length && usernames[i].substr(0,searchTerm.length) != searchTerm; i++);

You are checking the length of usernames and the length of searchTerm as well as getting a substr from usernames[i] EVERY time the loop uh, loops.

For any of these that you don't expect to change during the course of the loop, you should store them in a variable before the loop starts.

Getting a value out of a variable is much faster than checking an object property or method.

So something like:

for(i = 0,ii=usernames.length,j=searchTerm.length; i < ii && usernames[i].substr(0,j) != searchTerm; i++);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文