哪个是更高效的JavaScript数组搜索还是像MySQL这样的查询?

时间:2022-03-28 01:13:00

I know this is a somewhat nebulous question but which algorithm would be more efficient and/or "faster"?

我知道这是一个有点模糊的问题,但是哪种算法更有效和/或“更快”?

Searching through a preloaded JavaScript array of names or using the MySQL query "SELECT NAME WHERE NAME LIKE '%VARIABLE%'? The MySQL would be called using jquery AJAX to access a PHP file.

搜索一个预先加载的JavaScript数组或使用MySQL查询“选择名称,如%VARIABLE%”?使用jquery AJAX调用MySQL访问PHP文件。

A little background on my website, it is a site primarily for mobile users and uses a mix of jquery and php to deliver content. In this case the user would be searching for a specific name within a list of ~22,000 unique names.

在我的网站上有一点背景知识,它主要是为移动用户提供的,使用jquery和php混合提供内容。在这种情况下,用户将在~22,000个唯一名称的列表中搜索特定名称。

I have also thought about storing the table using LocalStorage as a cache, but then that requires an additional (though maybe less costly?) step of verifying/updating/loading the cache.

我还考虑过使用LocalStorage作为缓存来存储表,但是这需要一个额外的(虽然成本可能更低?)步骤来验证/更新/加载缓存。

If more information is needed please let me know and thanks in advance.

如果需要更多的信息,请提前告诉我并感谢。

EDIT:

编辑:

A bit of extra information, the user can and probably will be searching for multiple non-unique values. For example: a search value of 'Jane' can and should return the results 'Jane Smith', 'Janet Smith', 'Jane Doe', 'Janess Whatver', 'jfhfuhd_JANE_dfifhf, 'Blah'(don't know who would ever have a name like that but if it was in the database and they searched Jane it should be return along with the others)

再加上一点额外的信息,用户就可以而且可能会搜索多个非唯一值。例如:搜索“简”的价值可以而且应该返回结果“简·史密斯”,“珍妮特·史密斯”,“简”,”琼斯Whatver’,”jfhfuhd_JANE_dfifhf,“废话”(不知道是谁会有一个名字,但如果它是在数据库和他们搜查了简应该返回连同其他的)

2 个解决方案

#1


3  

This is just speculation, but without testing, immediately I'd go for the MySQL query. MySQL has been optimised by its developers for the best search/sort algorithms - that's its job (one look at their benchmark manual will give you an idea as to just how much they care). The people who write the JavaScript interpreters won't have had as much time to devote to optimising their search/sort algorithms.

这只是猜测,但没有测试,我立即使用MySQL查询。MySQL已经被开发人员优化为最好的搜索/排序算法——这是它的工作(看看他们的基准手册就会知道他们到底有多关心)。编写JavaScript解释器的人不会花那么多时间来优化他们的搜索/排序算法。

As an added plus, the Javascript option is dependent on the client's computer speed and browser - a slow device with a poorly implemented interpreted will take a much longer time. However, the MySQL option depends on your server, and therefore is completely under your control.

此外,Javascript选项还依赖于客户机的计算机速度和浏览器——一个运行缓慢的设备,如果解释得不好,将需要更长的时间。但是,MySQL选项取决于服务器,因此完全在您的控制之下。

Some tests

一些测试

JavaScript array with 10,000 values, comparing against a set value (jsfiddle.net/c6rpK/) - I get approximately 12ms

具有10,000个值的JavaScript数组,与设置值(jsfiddle.net/c6rpK/)相比,我得到了大约12ms

For the same test using MySQL I get 2.3ms

对于使用MySQL的相同测试,我得到2.3ms

#2


3  

I'd go with MySQL. In fact, I wouldn't even think of trying to compare the two.

我和MySQL。事实上,我甚至没有想过要比较这两者。

Databases are good at handling data: it's their job. Let them do it.

数据库擅长处理数据:这是他们的工作。让他们这样做。

Storing the data locally has quite some disadvantages:

在本地存储数据有一些缺点:

  • What if a piece of data changes? Will you re-post all data to all clients again?
  • 如果一段数据改变了怎么办?你会把所有的数据重新发布给所有的客户吗?
  • You have to send all data to all clients to begin with.
  • 您必须将所有数据发送到所有客户端。
  • Will the client have room (reserved) for that data?
  • 客户是否为该数据预留房间?

The best of all of course is to index the names, either completely or partially.

最好的方法当然是将名字全部或部分索引起来。

#1


3  

This is just speculation, but without testing, immediately I'd go for the MySQL query. MySQL has been optimised by its developers for the best search/sort algorithms - that's its job (one look at their benchmark manual will give you an idea as to just how much they care). The people who write the JavaScript interpreters won't have had as much time to devote to optimising their search/sort algorithms.

这只是猜测,但没有测试,我立即使用MySQL查询。MySQL已经被开发人员优化为最好的搜索/排序算法——这是它的工作(看看他们的基准手册就会知道他们到底有多关心)。编写JavaScript解释器的人不会花那么多时间来优化他们的搜索/排序算法。

As an added plus, the Javascript option is dependent on the client's computer speed and browser - a slow device with a poorly implemented interpreted will take a much longer time. However, the MySQL option depends on your server, and therefore is completely under your control.

此外,Javascript选项还依赖于客户机的计算机速度和浏览器——一个运行缓慢的设备,如果解释得不好,将需要更长的时间。但是,MySQL选项取决于服务器,因此完全在您的控制之下。

Some tests

一些测试

JavaScript array with 10,000 values, comparing against a set value (jsfiddle.net/c6rpK/) - I get approximately 12ms

具有10,000个值的JavaScript数组,与设置值(jsfiddle.net/c6rpK/)相比,我得到了大约12ms

For the same test using MySQL I get 2.3ms

对于使用MySQL的相同测试,我得到2.3ms

#2


3  

I'd go with MySQL. In fact, I wouldn't even think of trying to compare the two.

我和MySQL。事实上,我甚至没有想过要比较这两者。

Databases are good at handling data: it's their job. Let them do it.

数据库擅长处理数据:这是他们的工作。让他们这样做。

Storing the data locally has quite some disadvantages:

在本地存储数据有一些缺点:

  • What if a piece of data changes? Will you re-post all data to all clients again?
  • 如果一段数据改变了怎么办?你会把所有的数据重新发布给所有的客户吗?
  • You have to send all data to all clients to begin with.
  • 您必须将所有数据发送到所有客户端。
  • Will the client have room (reserved) for that data?
  • 客户是否为该数据预留房间?

The best of all of course is to index the names, either completely or partially.

最好的方法当然是将名字全部或部分索引起来。