js查询对象集合返回索引_对象索引与数组集合

js查询对象集合返回索引

设置和目标 (The Setup & Goal)

Let's say that we have one large text document and we have a a bunch of keywords that we want to parse the document for.  We don't care how many times times the keyword appears -- we just care that it's been used.  When we find a keyword, we need to record that keyword been found so that we may check at a later time.

假设我们有一个大的文本文档,并且有很多我们要为其解析文档的关键字。 我们不在乎关键字出现多少次,我们只是在乎它是否被使用过。 当我们找到一个关键字时,我们需要记录该关键字已被找到,以便我们稍后再检查。

低效的方法:数组收集和搜索 (Inefficient Method:  Array Collection and Search)

The first method to record that a keyword has been found is by pushing the keyword into one array:

记录已找到关键字的第一种方法是将关键字推入一个数组中:


//Assume an array called "foundKeywords" was defined above
if(shouldSave(keyword)) {
	foundKeywords.push(keyword);
}


At the end of the document search, we'd end up with an array like:

在文档搜索结束时,我们将得到一个类似以下的数组:


//the foundKeywords array looks like:
//['keyword1','keyword2','keyword2',...]


When it comes to checking this array for the existence of a given keyword, this method will prove inefficient.  Why?  Because we'd need to loop through the array and search until we found the given keyword (if ever).  Those are a lot of "wasted" or fruitless cycles, even if we break the loop when the keyword was found.  Inefficient is the only word to describe this process.

当检查此数组中是否存在给定关键字时,该方法将被证明效率不高。 为什么? 因为我们需要遍历数组并搜索,直到找到给定的关键字(如果有的话)。 即使我们在找到关键字时中断了循环,这些循环还是很多“浪费”或徒劳的循环。 低效率是描述此过程的唯一词。

高效方法:带有索引的对象 (The Efficient Method:  Object with Index)

The fastest method of checking a storing keywords for later reference is by object (in JavaScript) or associative array (in PHP).  Instead of adding the keyword to an array, we add the keyword as an index to a master object, giving the value as 1:

检查存储关键字以供以后参考的最快方法是按对象(在JavaScript中)或关联数组(在PHP中)。 我们没有将关键字添加到数组中,而是将关键字添加为主对象的索引,其值为1:


//Assume an object {} called "foundKeywords" was defined above
if(shouldSave(keyword)) {
	foundKeywords[keyword] = 1;
}


Why is this faster?  No wasted cycles looking blindly through an array.  The check is quick and simple:

为什么这样更快? 没有浪费的周期盲目地查看阵列。 检查快速简单:


if(foundKeywords[keyword]) { //FOUND!
	//do something
}


It's either an index or it's not!  In PHP, we'd save the keyword to an associative array:

它要么是索引,要么不是! 在PHP中,我们将关键字保存到关联数组中:


//Assume an array called "$found_keywords" was defined above
if(shouldSave($keyword)) {
	$found_keywords[$keyword] = 1;
}

//Later: checking if the keyword was there...
if($found_keywords[$keyword]) { //or array_key_exists($keyword,$found_keywords)
	//FOUND!
}


In a word...awesome.  Not only fast but simple!

一句话...很棒。 不仅快速而且简单!

I cannot provide a benchmark because the speed of execution depends on how large the keyword array is.  Suffice to say, for the sake of simplicity and speed, using an object with keyword index is definitely the way to go!

我无法提供基准,因为执行速度取决于关键字数组的大小。 可以说,为了简单和快速,使用带有关键字索引的对象绝对是必经之路!

翻译自: https://davidwalsh.name/object-array-index

js查询对象集合返回索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值