TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Trivial JavaScript: (a || b) vs (a && b)

1 点作者 samholmes大约 12 年前

1 comment

to3m大约 12 年前
I couldn't understand how !a&#38;&#38;!b would be slower; the NOT should be included in the branch.<p>The Firefox JIT inspector suggested that the body of the loop was being entirely removed, which makes sense, because it doesn't do anything. A body more like this does the trick:<p><pre><code> if(!a&#38;&#38;!b) {tt+=1;} else {tf+=1;} </code></pre> You'll need to add the relevant declarations to the setup section.<p><pre><code> var tt=0,tf=0; </code></pre> (The assignment to a and b should probably go there, too - it's in the teardown section at the moment.)<p>The result, though, is still the same!<p>I fixed this by switching the bodies round, so that !a&#38;&#38;!b runs first, then !(a||b) runs second. Now !a&#38;&#38;!b is quicker, as it should be, and I feel vindicated. Sort of.