mongdb选择存储引擎:
One lessor known fact about CSS selectors, querySelectorAll, and JavaScript-based selector engines is that they read your selectors from right to left. This news hit me as illogical at first, as you'd think that the first element in a selector string like "#myElement a.something .else" would provide a base context, but no: the ".else" is search for first. After more thought, searching for the right-most selector piece makes sense, as you instead collect the ".else" elements first (instead of, theoretically, all the elements under "#myElement", then "a.something" elements, and so on) and then look up the chain for matches. Essentially, you grab all the potential matches and then confirm by walking up the DOM tree, instead of grabbing the parent and look for matches on the way down.
众所周知,关于CSS选择器,querySelectorAll和基于JavaScript的选择器引擎的一个事实是,它们从右到左读取选择器。 这个消息起初使我感到不合逻辑,因为您会认为选择器字符串中的第一个元素(例如“ #myElement a.something .else”)将提供基本上下文,但没有:“。else”是首先搜索。 经过深思熟虑,搜索最右边的选择器很有意义,因为您可以先收集“ .else”元素(而不是理论上收集“ #myElement”下的所有元素,然后收集“ a.something”元素,依此类推),然后在链中查找匹配项。 本质上,您要抓住所有可能的匹配项,然后通过爬上DOM树进行确认,而不是抓住父项并在向下寻找匹配项。
I was recently looking at popular development site and found the following snippet:
最近,我在一个热门的开发网站上查看了以下代码段:
jQuery("#subscribe-main li:nth-child(4)")....
This snippet found the desired elements in 1ms according to FireBug's console. A millisecond is lightning fast, but if you slightly change the selector code, you get a faster result:
该片段根据FireBug的控制台在1毫秒内找到了所需的元素。 毫秒很快,但是如果稍微更改选择器代码,则会得到更快的结果:
jQuery("li:nth-child(4)", "#subscribe-main");// Could use this as well// jQuery("#subscribe-main").find("li:nth-child(4)")....
The selection code above returns the same elements in 0ms. A millisecond difference is negligible in one instance, but in a large application, these milliseconds will add up!
上面的选择代码在0ms内返回相同的元素。 在某些情况下,毫秒的差异可以忽略不计,但是在大型应用程序中,这些毫秒相加!
This post simply acts a a reminder that selector composition is important. Here's a task for you: go to the sites you've written the JavaScript selectors for and compare your selectors like I have above. If you aren't familiar with basic selector time testing via the console, here's how you do it:
这篇文章只是提醒人们选择器的组成很重要。 这是您的任务:转到您为JavaScript选择器编写的网站,然后像上面一样比较选择器。 如果您不熟悉通过控制台进行的基本选择器时间测试,请按以下步骤进行:
console.time("someKey");jQuery("#mySelector .more .stuff")...console.timeEnd("someKey");
The console doesn't do better than millisecond precision, but a different result at that precision gets you started in selector enhancement. Happy selector revising!
控制台的精度并没有比毫秒精度更好,但是以这种精度产生的不同结果使您开始进行选择器增强。 选择器修改愉快!
翻译自:
mongdb选择存储引擎: