我只是试图看到使用||
(双管道,短路运算符,应该更快)而不是|
的性能差异。
但结果并未对实际的性能改进有所了解。如果我们只是重新排序以下代码...首先使用|
执行控制台日志,那么它将显示比另一个更多的时间。
如何使用此方式或其他方式查看性能差异?此外,
当我们改变订单时,有人可以说明为什么会有差异?
如果performance.now();
表现得像这样,我在使用它时没有任何意义。
请帮忙。谢谢:))
var t3 = performance.now();
console.log(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (console.log(3)),
1 || (Infinity))
var t4 = performance.now();
console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
var t0 = performance.now();
console.log(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (console.log(3)),
1 | (Infinity))
var t1 = performance.now();
console.log("Call with single | took " + (t1 - t0) + " milliseconds.");

答案 0 :(得分:2)
- 如何使用此方式或任何其他方式查看性能差异?
- 有人可以解释为什么我们改变订单会有区别吗?
醇>
console.log
)都需要初始化,这将在第一次使用时完成,这很容易将测量的性能中断几个数量级。|
与||
,请不要在正在计时的区块中包含console.log
。一些示威:
多次运行以下代码。观察:
var t3 = performance.now();
console.log("herp");
var t4 = performance.now();
var t0 = performance.now();
console.log("derp");
var t1 = performance.now();
console.log("Call with herp took " + (t4 - t3) + " milliseconds.");
console.log("Call with derp took " + (t1 - t0) + " milliseconds.");

还要注意I / O对性能有可怕的影响:(你需要向下滚动)
var discard = function(){}; // just discards everything
var t3 = performance.now();
for(var i = 0; i < 1000; ++i)
{
discard("");
}
var t4 = performance.now();
var t0 = performance.now();
for(var i = 0; i < 1000; ++i)
{
console.log("");
}
var t1 = performance.now();
console.log("discard() took " + (t4 - t3) + " milliseconds.");
console.log("console.log() took " + (t1 - t0) + " milliseconds.");
&#13;
考虑到所有这些因素,让我们编写一个测试您的操作的测试用例:
var discard = function(){};
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (console.log(3)),
1 || (Infinity));
}
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (console.log(3)),
1 | (Infinity));
}
var t3 = performance.now();
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (console.log(3)),
1 || (Infinity));
}
var t4 = performance.now();
var t0 = performance.now();
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (console.log(3)),
1 | (Infinity));
}
var t1 = performance.now();
console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
&#13;
请注意,完成|
案例所需的所有时间仍然来自console.log
,证明||
上的短路确实有效。当然,这对于实际涉及I / O的|
vs ||
操作来说是准确的。但是,如果你把它排除在外,结果会再次接近:
(请注意,我将for循环调整为1000万,证明实际需要多长时间 console.log
!)
var discard = function(){};
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (Infinity));
}
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (Infinity));
}
var t3 = performance.now();
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (Infinity));
}
var t4 = performance.now();
var t0 = performance.now();
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (Infinity));
}
var t1 = performance.now();
console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
&#13;
尽管如此,|
和||
完全不同。不仅在执行方面,而且在结果方面。请尝试1 || 126
vs 1 | 126
或"a" || "b"
vs "a" | "b"
。