如何存储大量独特的条目?

发布于 2025-01-10 12:01:06 字数 146 浏览 1 评论 0原文

我正在尝试一个项目,该项目要求我记录用户访问的每个唯一的 URL,目标是有一种方法来存储每个数组以保持它们的唯一性。

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

显然,这在理论上可能是巨大的,如果它是一个数组,则可能有数万个条目,这是不行的——我该如何存储它们?

I am attempting a project which requires me to record each unique URL visited by the user, with the goal being to have a way of storing each array to keep them unique.

Is there an efficient way to do this?

Obviously this could be theoretically massive, possibly tens of thousands of entries if it was an array which would not do - how might I go about storing them?

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

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

发布评论

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

评论(1

此刻的回忆 2025-01-17 12:01:06

...如果它是一个数组,则可能有数万个条目,这是不行的...

数组可以包含数万甚至数十万个元素。

但为了唯一性,您需要一个 Set< /code>而不是数组,因为与数组上的线性查找时间相比,它提供了亚线性查找时间,并且为唯一值集提供了更好的语义。

这是一个构建一百万个唯一值集的示例-随机的数字:

const set = new Set();
console.time("Time to build the set");
while (set.size < 1_000_000) {
    set.add(Math.random()); // Won't add duplicate elements
}
console.timeEnd("Time to build the set");
console.log(`The set contains ${set.size.toLocaleString()} unique otherwise-random numbers`);

对我来说,运行时间约为 200 毫秒。而在几分钟后我放弃了使用数组的等效方法。

...possibly tens of thousands of entries if it was an array which would not do...

Arrays can contain tens of thousands, or even hundreds of thousands, of elements.

But for uniqueness, you want a Set rather than an array, as it offers sub-linear lookup time compared to linear lookup time on an array, and better semantics for sets of unique values..

Here's an example building a set of a million unique otherwise-random numbers:

const set = new Set();
console.time("Time to build the set");
while (set.size < 1_000_000) {
    set.add(Math.random()); // Won't add duplicate elements
}
console.timeEnd("Time to build the set");
console.log(`The set contains ${set.size.toLocaleString()} unique otherwise-random numbers`);

For me, that runs in about 200ms. Whereas I gave up on the equivalent using an array after several minutes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文